Fixed bug saving inlineformset_factory formsets
|Reported by:||Simon Litchfield||Owned by:||nobody|
|Has patch:||yes||Needs documentation:||no|
|Needs tests:||yes||Patch needs improvement:||no|
The inlineformset_factory formset save() method was setting it's fk using the 'myfk_id' no problem, but then overwriting it with form data that used 'myfk'. For new records, the form's 'myfk' is empty, so up comes an IntegrityError since null fk's were attempting to be saved. This same code used to work fine, so I assume something has fallen inconsistent around ModelForm field naming, or fk.name/fk.get_attname().
class App(models.Model): company = models.CharField(max_length=100, verbose_name='company name') ... class AppMember(models.Model): app = models.ForeignKey(App, related_name='members') name = models.CharField(max_length=50) ... AppMemberFormSet = inlineformset_factory(App, AppMember, extra=10) ... formset = AppMemberFormSet(data=request.POST or None) ... formset.instance = app # A saved app instance with an ID formset.save() # Error