0

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?

2
  • 1
    because forms.ModelForm doesn't recognize keyword request but in the first case you remove it by pop Commented Oct 26, 2019 at 19:36
  • Because then kwargs will still contain request. Commented Oct 26, 2019 at 19:39

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.