14 | | I'm using an inlineformset_factory where the parent model is SomeModel and the child model is C. |
15 | | When calling formset.save() I'm getting an error from django/forms/models.py (line 910) |
| 12 | Master / Detail type models (e.g. Order and OrderDetail) |
| 13 | |
| 14 | {{{ |
| 15 | class MasterModel(models.Model): |
| 16 | ... |
| 17 | |
| 18 | |
| 19 | class DetailModel(models.Model): |
| 20 | |
| 21 | master = models.ForeignKey(MasterModel, ...) |
| 22 | concrete_base = models.ForeignKey(ConcreteBase, ...) |
| 23 | }}} |
| 24 | |
| 25 | Form definition |
| 26 | |
| 27 | {{{ |
| 28 | class MasterModelForm(forms.ModelForm): |
| 29 | ... |
| 30 | |
| 31 | |
| 32 | class DetailModelForm(forms.ModelForm): |
| 33 | ... |
| 34 | # master excluded from fields as will be manually added in view |
| 35 | ... |
| 36 | }}} |
| 37 | |
| 38 | Formset definition |
| 39 | |
| 40 | {{{ |
| 41 | DetailModelFormset = inlineformset_factory(MasterModel, DetailModel, form=DetailModelForm) |
| 42 | }}} |
| 43 | |
| 44 | View definition |
| 45 | |
| 46 | {{{ |
| 47 | from django.views.generic.edit import CreateView #### <---- the error only happens on create view of MasterModel |
| 48 | |
| 49 | class MasterModelAdd(CreateView): |
| 50 | model = MasterModel |
| 51 | form_class = MasterModelForm |
| 52 | ... |
| 53 | |
| 54 | def form_valid(self, form): |
| 55 | master_instance = form.save() |
| 56 | detail_formset = DetailModelFormset()(self.request.POST) |
| 57 | detail_instances = detail_formset.save(commit=False) ### <------ this line raises the error |
| 58 | for detail_instance in detail_instances: ### <----- it never gets here |
| 59 | detail_instance.master = master_instance |
| 60 | detail_instance.save() |
| 61 | |
| 62 | }}} |
| 63 | |
| 64 | When calling formset.save() I'm getting an error from django/forms/models.py (line 910) regardless of the value of commit passed. |