I am trying to pass a filter queryset for a Django ModelForm widget (select widget, to be precise) (from a class based view.)
Normally, I would do something like
# views.py
class MyView(SuccessMessageMixin, CreateView):
model = MyModel
form_class = MyCreateForm
template_name = 'create.html'
success_message = "You new object has been created."
success_url = reverse_lazy('mymodel-index')
# forms.py
class MyCreateForm(forms.ModelForm):
class Meta:
model = MyModel
fields = [
'title', 'car', # etc
]
widgets = {
'car': forms.widgets.Select(
required=True,
queryset=Car.objects.all(), # I need to filter this to the current user
),
}
Or for non-select fields, I can pass initial data from the view class.
However, I need to filter the queryset (in the car
select Widget) by the currently logged in user. I could try passing the user to __init__
from the View, but I don't know how to access that from the Meta
subclass.
Any pointers in the right direction would be appreciated.