django one form, two models
This post is a work in progress is now working I am glad to say. I have been working on a django site which needs two models updated for one post. It is actually using models very close to that on django-forums and I have created a forms.py file:
class ThreadForm(forms.ModelForm):
class Meta:
model = Thread
exclude = ('forum', 'sticky', 'closed', 'posts', 'views', 'latest_post_time')
def clean_title(self):
title = self.cleaned_data['title']
if not alnum_re.search(title):
raise forms.ValidationError(ugettext("Titles can only contain letters, numbers and underscores"))
if len(title) < 1:
raise forms.ValidationError(ugettext("Please enter a title"))
return title
class PostForm(forms.ModelForm):
class Meta:
model = Post
exclude = ('thread', 'author', 'time', 'related_item')
def clean_body(self):
body = self.cleaned_data['body']
if len(body) < 1:
raise forms.ValidationError(ugettext("Please enter some body text"))
return body
And then in my views (after importing in the correct models):
@login_required
def groupnewthread(request, slug):
thegroup = get_object_or_404(GroupsOfUser, slug=slug)
if request.method == 'POST':
f = request.POST.copy()
tdata = {
'title': f['title'],
}
pdata = {
'body': f['body'],
}
t = ThreadForm(tdata)
p = PostForm(pdata)
if t.is_valid():
newthread = t.save(commit=False)
newthread.forum = thegroup
if p.is_valid():
newthread.save()
newpost = p.save(commit=False)
newpost.thread = newthread
newpost.author = request.user
newpost.save()
strmessage = 'has created a thread %s‘ % (newthread.get_absolute_url(), newthread.title)
usm = UserStatus(user = newpost.author, message = strmessage)
usm.save()
return HttpResponseRedirect(reverse(’groupdetail’, args=[thegroup.slug]))
else:
t = ThreadForm()
p = PostForm()
objContext = RequestContext(request, {’threadform’: t, ‘postform’: p})
return render_to_response(’groups/group_thread_add.html’, objContext)
Now the bit that is in progress is the returning part of dealing with the form data being bound, but I am going to write a custom handler. This is obviously going to be much easier to handle than an update, which I will have to deal with at a point.
The form HTML looks like this btw:
{% extends "base.html" %}
{% load i18n %}
{% block title %}
{% trans 'Add a new group thread' %}
{% endblock %}
{% block body %}
{% trans ‘Create a new group thread’ %}
{% if t.errors %}
{% blocktrans count t.errors|length as count %}Please correct the following error:{% plural %}Please correct the following errors:{% endblocktrans %}
{% endif %}
{% if p.errors %}
{% blocktrans count p.errors|length as count %}Please correct the following error:{% plural %}Please correct the following errors:{% endblocktrans %}
{% endif %}
{% endblock %}





