marcosgue
marcosgue

Reputation: 697

Django - how to exclude a form field view?

I have a problem to exclude a field if the user has a specific attribute.

I try to make test using the is_staff option following this tip, but I can't make it work. I get the error: __init__() got an unexpected keyword argument 'user'

Can anyone tell me how to exclude some fields if the user is staff please?

This is my form in forms.py

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):
        super(NewOrderForm, self).__init__(*args, **kwargs)
        user = kwargs.pop('user')
        if user.is_staff:
           del self.fields['store_internal_order']

and this is my view

def new(request):
    if not request.user.is_authenticated():
        return redirect('auth_login')

    title = "Nueva Orden de trabajo"
    store = request.user.userprofile.fk_store
    form = NewOrderForm(request.POST or None)
    context = {"title": title,
               "form": form}

    if 'dni_cuit' in request.session:
        dni_cuit = request.session.get('dni_cuit')
        if Client.objects.get(dni_cuit=dni_cuit):
            dni_cuit = request.session.pop('dni_cuit')
            client = Client.objects.get(dni_cuit=dni_cuit)

            context.update({'client': client})
            request.session['dni_cuit'] = dni_cuit
        else:
            return redirect('new_client')

    # Si el formulario es valido guardamos el contenido
    if form.is_valid():
...
...
...

Thanks very much who can help me.

Sorry for my bad english!

Upvotes: 3

Views: 1805

Answers (2)

Shang Wang
Shang Wang

Reputation: 25539

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']

Upvotes: 3

thevengefulco
thevengefulco

Reputation: 309

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?

Upvotes: 0

Related Questions