H
H
Hadmi2018-05-30 13:30:20
Django
Hadmi, 2018-05-30 13:30:20

Passing the current user to Forms.py - what's wrong?

Actually, there was already an answer here that it is necessary to use __init__, but I apparently do not fully understand how it works at all and my code does not start.

from django import forms
from .models import Container
from .my_defs import tree_parse

class ContainerForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        self.user = kwargs.pop('user',None)
        super(ContainerForm, self).__init__(*args, **kwargs)


    in_container_id = forms.ChoiceField(choices=tree_parse(0, 'choice', user))
    class Meta:
        model = Container
        fields = ('title', 'descripton', 'in_container_id')

My Containers objects form a tree structure, tree_parse is a function of which the id of the container for which the tree is to be built (0), the format of the tree ('choce') and the user who makes the request is passed, in response it returns a list suitable for insertion into the ChoiceField .
The error NameError: name 'user' is not defined is thrown.
Please explain what am I doing wrong? Thanks

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ilyas, 2018-05-30
@hadmi

where you call this form should be something like this
form = ContainerForm(user=request.user)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question