I'm using ModelChoiceField to select Invoice provider in my forms. In the current case, I'm returning all the Providers to the queryset.
But I should return only Providers that created by the signed user. Provider has a ForeignKey field named user
.
Here is my Form:
class InvoiceCreationForm(forms.Form):
# ...
provider = forms.ModelChoiceField(
label='Provider',
required=False,
queryset=Provider.objects.all(),
widget=forms.Select(attrs={
'name': 'provider',
'class': 'form-control',
'id': 'input-invoice-provider',
})
)
I need to filter objects like this:
Provider.objects.filter(user=current_user)
How can I get the signed user? Or how can I get the request
object outside of a view? I tried get the current user from __init__
method of the form then set it to self.user
. But I can't use it in the query like self.user
.