#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.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/049.281714481dde7b64eb943276c10fb476%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to