[Django]-Django BaseGenericInlineFormSet forms not inheriting FormSet instance as form instance related_object

3👍

Since I’ve got no feedback, I’ll assume it’s a Django bug, and it really seems to be the case. I filed a ticket here: https://code.djangoproject.com/ticket/25488

The solution until this is solved is either what I suggested earlier (i.e. iterating over the forms in the view and linking them to the product manually) or using a fixed FormSet class, something like:

class FixedBaseGenericInlineFormSet(BaseGenericInlineFormSet):
    def _construct_form(self, i, **kwargs):
        form = super()._construct_form(i, **kwargs)
        setattr(form.instance, self.ct_field.get_attname(),
            ContentType.objects.get_for_model(self.instance).pk)
        setattr(form.instance, self.ct_fk_field.get_attname(),
            self.instance.pk)
        return form

ProductImageInlineFormset = generic_inlineformset_factory(
    Image,
    form=ProductImageForm,
    formset=FixedBaseGenericInlineFormSet,
    extra=1
)
👤Ariel

Leave a comment