django-polymorphic icon indicating copy to clipboard operation
django-polymorphic copied to clipboard

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

Open ImanoWilliams opened this issue 6 years ago • 2 comments

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)

ImanoWilliams avatar Sep 12 '18 16:09 ImanoWilliams

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.

wfehr avatar Oct 24 '18 13:10 wfehr

@wfehr were you able to solve it?

ImanoWilliams avatar Feb 20 '19 13:02 ImanoWilliams