Reputation: 1825
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?
Upvotes: 3
Views: 1358
Reputation: 5442
I'm not sure this is a good solution, but it's the only solution I can come up with that will work.
I would create a List of Dictionaries. Each Dictionary will have a baseForm key which will hold your B class form and a inline_forms key which will hold your class A inline_formset.
In your view code iterate over the list and print the forms accordingly. Generate a different value for each of the forms submit button. For example submit_1 for the first form , etch ..
When a form is submitted, check this value (the value of the submit button), if the value is for example submit_2 you are certain the second item/form of your list has been submitted. From their you can start processing the submitted data.
Upvotes: 0
Reputation: 5442
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/
Upvotes: 1