Skip to content

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

@ImanoWilliams

Description

@ImanoWilliams

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions