﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
28311	Ability to specify field querysets in ModelForm constructor	Pascal Briet	nobody	"Hi,

There is a common problematic I often encounter : Displaying a ModelForm with foreignkeys (ModelChoiceField) requiring sub-querysets.

e.g., I have a `Restaurant` model, that I first create in a `Country` (`Restaurant::country`). Then, in a form, I want to specify a `City` (`Restaurant::city`).

I do not want to display all the cities from all the world, but only the cities from the pre-selected country. (please note that the subselection logic might be more complex in many cases).

Usually, you do the following in Django :

{{{

class RestaurantForm(forms.ModelForm):
    class Meta:
        model = Restaurant
        fields = ['name', 'city']

    def __init__(self, *args, **kargs):
         super().__init__(*args, **kargs)
         self.fields['city'].queryset = City.objects.filter(country=self.fields['country'])

my_form = RestaurantForm(instance=restaurant)
}}}

It works, but it looks to me rather complicated for such a common use (especially for beginners).
Would it be possible to pass through the Form contructor a dictionary of querysets that would replace the default ones ?

{{{
my_form = RestaurantForm(instance=restaurant, field_querysets={'city': City.objects.filter(country=restaurant.country)}
}}}

Does it make sense ? It doesn't invalidate the first approach. The classic way bounds the logic to the form itself - which is very handy - but I believe the suggested way would make Django more accessible to newbies.

Thanks,"	New feature	closed	Forms	1.11	Normal	wontfix	modelform queryset		Unreviewed	0	0	0	0	0	0
