Answer the question
In order to leave comments, you need to log in
How to get request.user.id in django.forms?
forms.py
class WorkForm(forms.Form):
road = forms.ModelChoiceField(
label='Выберите объект',
queryset=Roads.objects.filter(contractor=request.user.id))
name 'request' is not defined
Answer the question
In order to leave comments, you need to log in
Define your own __init__
, where to give the desired parameter.
class WorkForm(forms.Form)
road = forms.ModelChoicField(label='', queryset=Road.objects.none())
def __init__(self, *args, **kwargs):
user_id = kwargs.pop('user_id', None)
super(WorkForm, self).__init__(*args, **kwargs)
self.fields['road'].queryset = Roads.objects.filter(contractor=user_id)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question