Django BaseGenericInlineFormSet forms not inheriting FormSet instance as form instance related_object

拥有回忆 提交于 2019-12-05 20:23:56

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
)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!