Closed mpnordland closed 6 years ago
I found a solution: don't use ModelFieldList and ModelForm on the top level form.
class BreakfastEggsForm(Form):
eggs = FieldList(FormField(EggForm))
Normal FieldList works just fine. It also works with a second layer of related objects, say spices related to an egg:
class SpiceForm(ModelForm):
class Meta:
model = Spice
class EggForm(ModelForm):
class Meta:
model = Egg
spices = FieldList(FormField(SpiceForm))
If your having issues with sqlalchemy.exc.IntegrityError 'Cannot add or update a child row: a foreign key constraint fails' and the foreign key to your parent model is missing, this may solve your problem.
Suppose I have a Breakfast model and among other fields, it has a one to many relationship with an Egg model. Suppose further that I have made a breakfast wizard, each page having a separate ModelForm with only the desired fields in it. Now, one of those ModelForms looks like this:
It's purpose is to edit the eggs related to a breakfast. When I call form.populate_obj(breakfast) the eggs in the collection suddenly have no id's on them. Their own id and their breakfast_id field is missing too.