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 file:

<br /> class ThreadForm(forms.ModelForm):<br /> class Meta:<br /> model = Thread<br /> exclude = (‘forum’, ‘sticky’, ‘closed’, ‘posts’, ‘views’, ‘latest_post_time’)</p> <p> def clean_title(self):<br /> title = self.cleaned_data[‘title’]<br /> if not<br /> raise forms.ValidationError(ugettext(“Titles can only contain letters, numbers and underscores”))</p> <p> if len(title) < 1:<br /> raise forms.ValidationError(ugettext(“Please enter a title”))<br /> return title</p> <p>class PostForm(forms.ModelForm):<br /> class Meta:<br /> model = Post<br /> exclude = (‘thread’, ‘author’, ‘time’, ‘related_item’)</p> <p> def clean_body(self):<br /> body = self.cleaned_data[‘body’]<br /> if len(body) < 1:<br /> raise forms.ValidationError(ugettext(“Please enter some body text”))<br /> return body<br />

And then in my views (after importing in the correct models):

<br /> @login_required<br /> def groupnewthread(request, slug):<br /> thegroup = get_object_or_404(GroupsOfUser, slug=slug)<br /> if request.method == ‘POST’:<br /> f = request.POST.copy()<br /> tdata = {<br /> ‘title’: f[‘title’],<br /> }<br /> pdata = {<br /> ‘body’: f[‘body’],<br /> }<br /> t = ThreadForm(tdata)<br /> p = PostForm(pdata)<br /> if t.is_valid():<br /> newthread =<br /> = thegroup</p> <p> if p.is_valid():<br /><br /> newpost =<br /> newpost.thread = newthread<br /> = request.user<br /></p> <p> strmessage = ‘has created a thread <a href=“%s”>%s</a>’ % (newthread.get_absolute_url(), newthread.title)<br /> usm = UserStatus(user =, message = strmessage)<br /></p> <p> return HttpResponseRedirect(reverse(‘groupdetail’, args=[thegroup.slug]))</p> <p> else:</p> <p> t = ThreadForm()<br /> p = PostForm()</p> <p> objContext = RequestContext(request, {‘threadform’: t, ‘postform’: p})<br /> return render_to_response(‘groups/group_thread_add.html’, objContext)<br />

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:

<br /> {% extends “base.html” %}<br /> {% load i18n %}<br /> {% block title %}<br /> {% trans ‘Add a new group thread’ %}<br /> {% endblock %}<br /> {% block body %}</p> <h1>{% trans ‘Create a new group thread’ %}</h1> <div class=“column span-14”> {% if t.errors %}</p> <h3>{% blocktrans count t.errors|length as count %}Please correct the following error:{% plural %}Please correct the following errors:{% endblocktrans %}</h3> <p> {% endif %}<br /> {% if p.errors %}</p> <h3>{% blocktrans count p.errors|length as count %}Please correct the following error:{% plural %}Please correct the following errors:{% endblocktrans %}</h3> <p> {% endif %}</p> <table>

</table> </div> <p>{% endblock %}<br />

comments powered by Disqus