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
provider = forms.ModelChoiceField(
Getting the user in
__init__() is only the first part of the solution. You also need to use that user to filter the queryset:
class InvoiceCreationForm(forms.Form): def __init__(self, *args, **kwargs): self.user = kwargs.pop('user', None) super(InvoiceCreationForm, self).__init__(*args, **kwargs) if self.user: self.fields['provider'].queryset = Provider.objects.filter(user=self.user)