[Django]-Django – how to exclude a form field view?

3👍

You are on the right track, but your statement user = kwargs.pop('user') is at the wrong position. It should be called before super(NewOrderForm, self).__init__(*args, **kwargs). The reason this error occurs is because in the base form class, __init__() method doesn’t expect to accept your user parameter. Thus, we need to pop it first and store it in a variable then call super.

After the correction:

class NewOrderForm(forms.ModelForm):

    class Meta:
        model = WorkOrder
        fields = ['store_internal_order',
                  'sector',
                  'article',
                  'serial',
                  'work',
                  'article_details',
                  'cash_advance',
                  'initial_price'
                  ]

    def __init__(self, *args, **kwargs):
        # user should be popped here
        user = kwargs.pop('user', None)
        super(NewOrderForm, self).__init__(*args, **kwargs)
        # just in case your user is empty
        if user and user.is_staff:
           del self.fields['store_internal_order']

0👍

Not sure if any of this helps but should user = kwargs.pop('user') be user = kwargs.pop('user', None)? Also did you try passing user=request.user as a parameter in NewOrderForm?

Leave a comment