#23250: Improve Error Message / Handling for ModelMultipleChoiceField and 
queryset
attribute
--------------------------------------+--------------------
     Reporter:  wraus                 |      Owner:  nobody
         Type:  Cleanup/optimization  |     Status:  new
    Component:  Forms                 |    Version:  master
     Severity:  Normal                |   Keywords:
 Triage Stage:  Unreviewed            |  Has patch:  0
Easy pickings:  0                     |      UI/UX:  0
--------------------------------------+--------------------
 I have a ModelMultipleChoiceField whose queryset I want to populate after
 the initialization of the form.

 {{{
 class FooMultipleChoiceForm(forms.Form)
     foo_select = forms.ModelMultipleChoiceField(
         label="Foo Selection",
         help_text="Select a Foo from the choices above.",
         queryset=None,
     )

     def __init__(self, bar, *args, **kwargs):
         super(FooMultipleChoiceForm, self).__init__(*args, **kwargs)
         self.fields['foo_select'].queryset = bar.foo_set.all()
 }}}


 Currently, there are two major issues with this:

 1. Even if I want to set the queryset later in {{{__init__}}} with
 {{{self.fields['foo_select']}}}, I still have to specify a queryset
 attribute when declaring the field, even though the given queryset will be
 overwritten in {{{__init__}}}. This not only seems redundant, but it feels
 odd to be forced to enter a value for the explicit purpose of having it
 overwritten later, especially when providing None for the queryset
 attribute doesn't cause any issues if a new queryset is provided in the
 {{{__init__}}} of the form.
 2. If I omit the queryset attribute in ModelMultipleChoiceField, I get a
 confusing error message:
 {{{
 TypeError: __init__() takes at least 2 arguments (3 given)
 }}}
    Nothing is mentioned about the missing queryset argument, and given
 that I'm setting it later, it's easy to overlook that it's the cause of
 the problem.

 Given that setting queryset to None doesn't cause problems as long as the
 queryset is initialized later in the form, it seems like it might make
 sense to allow queryset to be unspecified at field declaration time, as
 long as it's specified later.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/23250>
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/048.a5626b612492419b04157a5e076bcc87%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to