[Django] #28311: Ability to specify field querysets in ModelForm constructor

10 views
Skip to first unread message

Django

unread,
Jun 15, 2017, 5:22:00 AM6/15/17
to django-...@googlegroups.com
#28311: Ability to specify field querysets in ModelForm constructor
-------------------------------------+-------------------------------------
Reporter: Pascal | Owner: nobody
Briet |
Type: New | Status: new
feature |
Component: Forms | Version: 1.11
Severity: Normal | Keywords: modelform queryset
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
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,

--
Ticket URL: <https://code.djangoproject.com/ticket/28311>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jun 15, 2017, 8:36:45 AM6/15/17
to django-...@googlegroups.com
#28311: Ability to specify field querysets in ModelForm constructor
------------------------------------+--------------------------------------
Reporter: Pascal Briet | Owner: nobody
Type: New feature | Status: closed
Component: Forms | Version: 1.11
Severity: Normal | Resolution: wontfix
Keywords: modelform queryset | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
------------------------------------+--------------------------------------
Changes (by Tim Graham):

* status: new => closed
* resolution: => wontfix


Comment:

I don't see a need to add an alternate way of doing this. In my opinion,
it could lead to a slippery slope with proposals to add all sorts of all
other form customization options in `ModelForm.__init__()`. Also, I feel
the logic really does belong in the form, not in the view (or wherever the
form is initialized).

Feel free to propose the idea on the DevelopersMailingList if you disagree
with my assessment.

--
Ticket URL: <https://code.djangoproject.com/ticket/28311#comment:1>

Reply all
Reply to author
Forward
0 new messages