D
D
DennyD3142017-08-31 01:52:40
Django
DennyD314, 2017-08-31 01:52:40

Why does the RelatedObjectDoesNotExist error occur due to changes in the __init__ of the model when using admin.TabularInline?

Hello!
The question is, I want to change the choices of another depending on the value of one field of the model.
To do this, I override the __init__() method like this:

def __init__(self, *args, **kwargs):
        super(MyModel, self).__init__(*args, **kwargs)
        comp = self.composition #FOREIGN KEY
        if comp.for_two:
           self._meta.get_field_by_name('type')[0]._choices = self.COMP_MEMBER_TYPE_CHOICES_1
        else:
            self._meta.get_field_by_name('type')[0]._choices = self.COMP_MEMBER_TYPE_CHOICES

In the admin panel, the page for editing objects of this class is displayed correctly.
But when I try to use InLineAdmin, I get the RelatedObjectDoesNotExist error. What causes
this error and how can I fix it?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question