jazzband / django-polymorphic

Improved Django model inheritance with automatic downcasting
https://django-polymorphic.readthedocs.io
Other
1.65k stars 279 forks source link

Nested polymorphic_inline_formsets gives AttributeError: 'NoneType' object has no attribute 'get_real_instance_class' #363

Open ImanoWilliams opened 6 years ago

ImanoWilliams commented 6 years ago

Thank you creating this django package.

I am trying to create a nested inline formset with the innermost being the polymorphic inline formset

this my polymorphic inline formset

Oformset=polymorphic_inlineformset_factory(MBF, MO, 
                                           formset_children=(PolymorphicFormSetChild(LO),
                                                             PolymorphicFormSetChild(DO),
                                                             PolymorphicFormSetChild(BO),
                                                             PolymorphicFormSetChild(AO),
                                                             ),fields='__all__', can_delete=False, extra=0)

I created a regular baseinlineformset and added the Oformse, polymorphic_inlineformset, to it.

I "model = defaults['instance'].get_real_instance_class() # allow proxy models AttributeError: 'NoneType' object has no attribute 'get_real_instance_class' " for the form.nested in the is_valid()

class MyBaseFormset(BaseInlineFormSet):
    def add_fields(self, form, index):
        super(BaseScenarioFormset, self).add_fields(form, index)
        form.nested=Oformset(instance=form.instance,
                                  data=form.data if form.is_bound else None,
                                  files=form.files if form.is_bound else None,
                                  prefix='modelobject-%s-%s' % (form.prefix,
                                                                Oformset.get_default_prefix())
                                  )    

    def is_valid(self):
        result =super(BaseScenarioFormset, self).is_valid()     
        if self.is_bound:
            for form in self.forms:
                result=result and form.nested.is_valid()
        return result

    def save(self, commit=True):
        result=super(BaseScenarioFormset, self).save(commit=commit)
        for form in self:
            form.nested.save(commit=commit)
        return result

formset that will hold the polyinlineformset

ModelBasicFormSet=inlineformset_factory(UCP, MBF, fields=['modelNum','interactor', 'action', 
validationStep'], formset=MyBaseFormset, can_delete=False,extra=0)
wfehr commented 6 years ago

Having the same error with django-cms. When you edit a polymorphic-inline-model which gets deleted in the background and you safe it, the above error occurs.

ImanoWilliams commented 5 years ago

@wfehr were you able to solve it?