Sygol
Sygol

Reputation: 197

Understanding inheritance in Django

I am trying to understand some basics in django inheritance - I'm sure it is something trivial, buy I just can't get it.

I've got my CartItemForm(forms.ModelForm) and I override init method to get user from post.request, like that:

def __init__(self, *args, **kwargs):
    self.request = kwargs.pop('request', None)
    super().__init__(*args, **kwargs)

And it works, but I don't really get why it doesn't work when I inherit init method first:

def __init__(self, *args, **kwargs):
    super().__init__(*args, **kwargs)
    self.request = kwargs.pop('request', None)

init() got an unexpected keyword argument 'request'

What am I missing here?

Upvotes: 0

Views: 59

Answers (1)

Sayse
Sayse

Reputation: 43300

It doesn't work because the base class uses an explicit list of keyword args, and request isn't one of them

def __init__(self, data=None, files=None, auto_id='id_%s', prefix=None,
                 initial=None, error_class=ErrorList, label_suffix=None,
                 empty_permitted=False, instance=None, use_required_attribute=None,
                 renderer=None):

For completeness, it works before hand because you're pop-ing the request keyword out of the keyword dictionary and no longer exists when you're calling super

Upvotes: 3

Related Questions