Form_for multiple models

127 views
Skip to first unread message

Chris Hoeppner

unread,
Jul 24, 2007, 9:02:39 AM7/24/07
to Django Users List
Hi there!

I need to get a form done. The 'long' way.

Well... Actually, what I need is the same as form_for_instance, but for
more than a single model.

The actual form_for_instance is a bit too advanced for my poor python
knowledge. Any way to get this a bit easier than creating the form by hand?

signature.asc

Russell Keith-Magee

unread,
Jul 24, 2007, 9:17:37 AM7/24/07
to django...@googlegroups.com
On 7/24/07, Chris Hoeppner <ch...@pixware.org> wrote:

> Well... Actually, what I need is the same as form_for_instance, but for
> more than a single model.

Why not use multiple forms? There is nothing stopping you from putting
multiple forms into a context, and populating multiple forms from a
single POST dictionary. The only things to keep in mind are:

- remember to check validity for both forms (form1.is_valid() and
form2.is_valid())

- When you instantiate your form, pass in prefix='form1' as an
argument - all the fields on the form will get that prefix, which will
keep the two forms distinct in the POST dictionary. This will only be
an issue if there is an overlap in the field names on the two forms,
but it's better to be safe.

Yours,
Russ Magee %-)

Chris Hoeppner

unread,
Jul 24, 2007, 11:40:07 AM7/24/07
to django...@googlegroups.com
Russell Keith-Magee escribió:

If I get this right, I might just throw fields from different models
together in one <form>, and on the server side, call the save() method
on each form instance, and all the data gets where it belongs to?

signature.asc

Chris Brand

unread,
Jul 24, 2007, 2:05:50 PM7/24/07
to django...@googlegroups.com
> - When you instantiate your form, pass in prefix='form1' as an
> argument - all the fields on the form will get that prefix, which will
> keep the two forms distinct in the POST dictionary. This will only be
> an issue if there is an overlap in the field names on the two forms,
> but it's better to be safe.

I've seen this "prefix" mentioned before in this context. Is it in the
documention yet (I couldn't find it) ? Do all forms accept it as an
instantiation parameter, or is it specific to form_for_instance and
form_for_model ? Is it in 0.96 or only in trunk ?

A quick grep through the 0.96 source seems to indicate that it is a
parameter to BaseForm.__init__(), but that's as far as I can get by myself
at the moment...

Thanks,

Chris

Nathan Ostgard

unread,
Jul 24, 2007, 6:12:04 PM7/24/07
to Django users
You can specify it upon form creation. Here is an example of how you
could use prefixes for multiple model forms:

In your views:

from django import newforms as forms
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
from yourproject.yourapp.models import ModelClass1, ModelClass2

def some_form_view(request):
form_classes = {ModelClass1: None, ModelClass2: None}
for model in form_classes:
form_classes[model] = forms.form_for_model(model)
form_objects = {}
if request.method == 'POST':
prefix = 0
forms_are_valid = True
for model, form_class in form_classes.iteritems():
form_objects[model] = form_class(request.POST, prefix='f' +
str(prefix))
forms_are_valid = forms_are_valid and
form_objects[model].is_valid()
prefix += 1
if forms_are_valid:
for model, form in form_objects.iteritems():
form.save()
return HttpResponseRedirect('/some/url')
else:
prefix = 0
for model, form_class in form_classes.iteritems():
form_objects[model] = form_class(prefix='f' + str(prefix))
return render_to_response('blah.html', {'forms':
form_objects.values()})

In your template:

<form action="." method="post">
{% for form in forms %}
{{ form.as_p }}
{% endfor %}
<div class="submit"><input type="submit" value="Submit"></div>
</form>

Chris Brand

unread,
Jul 24, 2007, 6:17:10 PM7/24/07
to django...@googlegroups.com
> You can specify it upon form creation. Here is an example of how you
> could use prefixes for multiple model forms:

Thanks, Nathan.
That's very useful.

Chris

Reply all
Reply to author
Forward
0 new messages