So, my goal is to be able to filter a ModelChoiceField queryset in my ModelForm to only include Places that request.user has created.
My ModelForm is simply:
class PlaceEventForm(models.ModelForm):
class Meta:
model = Event
I'd like to be able to add something like:
def __init__(self, *args, **kwargs):
super(PlaceEventForm, self).__init__(*args, **kwargs)
self.fields['place'].queryset = Place.objects.filter(created_by=request.user)
However, I can't seem to find a way to access the request in the ModelForm.
My View is like so:
class PlaceEventFormView(CreateView):
form_class = PlaceEventForm
template_name = 'events/event_create.html'
@method_decorator(login_required)
def dispatch(self, *args, **kwargs):
return super(PlaceEventFormView, self).dispatch(*args, **kwargs)
I'm not sure if this is even close to what I should do, but I tried:
def get_form_kwargs(self):
kwargs = super(PlaceEventFormView, self).get_form_kwargs()
kwargs.update({'place_user': self.request.user})
return kwargs
But I got the error: init() got an unexpected keyword argument 'place_user'
Any ideas on this one? Or can anyone think of a way to filter my ModelChoiceField in the view without needing to pass my request to the ModelForm?