user1214179
user1214179

Reputation: 23

Multiple inheritance and Django Form

Why this code doesn't work? I see in debugger (PyCharm) that init line is executed but nothing more. I have tried to put there raise exception to be really sure and again nothing happend.

class polo(object):
    def __init__(self):
        super(polo, self).__init__()
        self.po=1     <- this code is newer executed

class EprForm(forms.ModelForm, polo):
    class Meta:
        model = models.Epr

Upvotes: 1

Views: 1005

Answers (1)

Mariusz Jamro
Mariusz Jamro

Reputation: 31633

You use multiple inheritance so in general Python will look for methods in left-to-right order. So if your class do not have __init__ it'll look for it in ModelForm and that (only if not found) in polo. In your code the polo.__init__ is never called because ModelForm.__init__ is called.

To call the constructors of both base classes use explicit constructor call:

class EprForm(forms.ModelForm, polo):

    def __init__(self, *args, **kwargs)
        forms.ModelForm.__init__(self, *args, **kwargs) # Call the constructor of ModelForm
        polo.__init__(self, *args, **kwargs) # Call the constructor of polo

    class Meta:
        model = models.Epr

Upvotes: 2

Related Questions