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:
To update Yuji's answer for Django 1.10+ (including Django 2.0+), see the example below (note the updated method signature). Yuji's suggested approach keeps the queryset in the view along with the other business logic, and helps keep any form class extending models.ModelForm clean and straightforward.
def get_form(self, form_class=None):
if form_class is None:
form_class = self.get_form_class()
form = super(MyView, self).get_form()
form.fields['place'].queryset = Place.objects.filter(created_by=self.request.user)
return form
Shorter:
def get_form(self, form_class=None):
form = super(MyView, self).get_form(form_class)
form.fields['place'].queryset = Place.objects.filter(created_by=self.request.user)
return form