Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

combine modelformset and inlineformset in django views

i have two models

   class A(models.Model):
        name = models.CharField(50)
        type = models.CharField(50)

   class B(models.Model):
        field1 = ForeignKeyField(A)
        value = IntegerField()

I'd like to display a number of forms via a ModelFormSet (from model A) where each one of the forms displays in turn InlineFormSets (from model B) for all objects connected to the object.

How can i combine both modelformsets (model A) and inline formsets (mdoel B) on save method in django views?

like image 340
Asif Avatar asked Jan 25 '26 12:01

Asif


1 Answers

I've struggled with this a week ago. I would suggest you start using the inlineformset_factory method: https://docs.djangoproject.com/en/dev/topics/forms/modelforms/

Then just add your base form and your formset to your view and render them.

Here's some blog post that helped me figure this out: http://charlesleifer.com/blog/djangos-inlineformsetfactory-and-you/

There's also a solution to make it work using Class Based Views: http://haineault.com/blog/155/

like image 63
Jonas Geiregat Avatar answered Jan 28 '26 02:01

Jonas Geiregat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!