More Than Two Models With inlineformset_factory

405 views
Skip to first unread message

Geraldo

unread,
Aug 11, 2009, 4:04:33 PM8/11/09
to Django users
Hi,

I'm new to Django and am putting together a page for my new site. I
want to be able to edit data that is contained in 3 models, organized
as follows:
Parent
--> Child 1 (always one to one)
--> Child 2 (one or more)

I understand I can do the following:

class Parent(models.Model):
name = models.CharField(max_length=100)
parent_type = models.CharField(max_length=10)

class Child1(models.Model):
name = models.ForeignKey(Author)
address = models.CharField(max_length=100)

class Child2(models.Model):
name = models.ForeignKey(Author)
gender = models.CharField(max_length=8)

# so I can do this to get a formset with Parent and Child1
Formset1 = inlineformset_factory(Parent, Child1)
dad = Parent.objects.get(name=u'Popeye')
formset = Formset1(instance=dad)

# or this to get a formset with Parent and Child2
Formset2 = inlineformset_factory(Parent, Child2)
dad = Parent.objects.get(name=u'Popeye')
formset = Formset2(instance=dad)

What I don't understand is how I get both child1 and child2 in my
formset?

Any help would be greatly appreciated. Thanks so much in advance!
geraldo

Matthias Kestenholz

unread,
Aug 11, 2009, 5:55:45 PM8/11/09
to django...@googlegroups.com

You can pass a prefix parameter when constructing the formsets (not
the formset classes):

Formset1 = inlineformset_factory(Parent, Child1)
Formset2 = inlineformset_factory(Parent, Child2)

if request.method == 'POST':
child1_formset = Formset1(request.POST, ..., prefix='child1')
child2_formset = Formset2(request.POST, ..., prefix='child2')
else:
child1_formset = Formset1(..., prefix='child1')
child2_formset = Formset2(..., prefix='child2')


This prefix will be added to all formset fields automatically. The
browser and Django will be able to differentiate between the two
formsets now.

Matthias

--
http://spinlock.ch/pub/feincms/

Geraldo

unread,
Aug 11, 2009, 6:51:33 PM8/11/09
to Django users
Excellent, Matthias... That should work nicely. It does appear,
however, that there is no way to have more than 2 forms in a single
formset. If inlineformset_factory had an append method things might
be a bit cleaner.

Thanks again for your help.
geraldo

On Aug 11, 3:55 pm, Matthias Kestenholz
<matthias.kestenh...@gmail.com> wrote:

Matthias Kestenholz

unread,
Aug 12, 2009, 2:53:56 AM8/12/09
to django...@googlegroups.com
On Wed, Aug 12, 2009 at 12:51 AM, Geraldo<quaki...@gmail.com> wrote:
>
> Excellent, Matthias...  That should work nicely.  It does appear,
> however, that there is no way to have more than 2 forms in a single
> formset.  If inlineformset_factory had an append method things might
> be a bit cleaner.
>

I presume you mean more than two forms of the same type (Child1 or
Child2)? That's easily possible, and we use it all the time. Take a
look at the max_num and extra parameters to the factory function.


Matthias

--
FeinCMS Django CMS building toolkit: http://spinlock.ch/pub/feincms/

Geraldo

unread,
Aug 12, 2009, 1:36:39 PM8/12/09
to Django users
No, I mean two or more different forms. I'd like Parent, Child1 AND
Child2 all in the same formset. What you've suggested though, should
get me where I want to go.

Thanks again,
Gerry

On Aug 12, 12:53 am, Matthias Kestenholz
<matthias.kestenh...@gmail.com> wrote:

Matthias Kestenholz

unread,
Aug 12, 2009, 4:15:28 PM8/12/09
to django...@googlegroups.com
On Wed, Aug 12, 2009 at 7:36 PM, Geraldo<quaki...@gmail.com> wrote:
>
> No, I mean two or more different forms.  I'd like Parent, Child1 AND
> Child2 all in the same formset.  What you've suggested though, should
> get me where I want to go.
>

Well, you can easily show the input fields from Formset1 and Formset2
intertwined. You need to output the mangement forms and the individual
fields by hand. It's very similar to outputting form elements by hand
instead of using the as_ul/as_table convenience methods.


Hope it helps,

Reply all
Reply to author
Forward
0 new messages