Bug in Django 2.1 on ModelMultipleChoiceField

379 views
Skip to first unread message

Odile Lambert

unread,
Mar 13, 2019, 2:57:58 PM3/13/19
to Django users

Hello,

I believe Bugs in Django are to be reported in this user list. Below is a report for a bug which does not seem to be already filed.

In the documentation reference on the ModelMultipleChoiceField there is no restrictios mentioned on the empty_label attribute.

Whenever you declare an attribute empy_label for a ModelMultipleChoiceField, it generates an error during the system_checks

 typeError " __init__() got multiple values for keyword argument 'empty_label

To reproduce the bug : :

class MyForm(forms.Form):
    field = forms.ModelMultipleChoiceField(
        queryset = Imodelclass.objects.all(),)
        empty_label=('------------'))

Another user on stackoverflow encountered  this same problem see here : https://stackoverflow.com/questions/52263426/django-empty-label-for-modelmultiplechoicefield-multiple-values-for-keyword-arg

The cause of the error is the following :

the code ( see fdjango/forms/models.py line 1266 sets the empty_label attribute to None while calling the super_class (ModelChoiceField):

super().__init__(queryset, empty_label=None, **kwargs)

while the super_class.__init__ sets the empy_label to "---------".

removing the empty_label= None in the call to the super_class solved the problem for me.

best regards

Odile Lambert


Matthew Pava

unread,
Mar 13, 2019, 5:07:15 PM3/13/19
to django...@googlegroups.com

Looking at it, I would say that this is by design. You shouldn’t use an empty label on a multiple choice field. Perhaps this should be clarified in the docs.

Saying that, if you really want an empty label on a multiple choice field, you could create your own class that initializes empty_label however you want it.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/063841b4-5b58-4931-6ea3-29188ccc8171%40laposte.net.
For more options, visit https://groups.google.com/d/optout.

Odile Lambert

unread,
Mar 14, 2019, 3:10:17 AM3/14/19
to django...@googlegroups.com, Matthe...@iss.com

Hello

If this is by design Django should raise an error indicating that ModelMultipleChoiceField does not allow an empty_label attribute

The present error message in the __init__ of the super_class is not coherent and very difficult to understand for beginners. I would strongly suggest that it is filed as a bug to improve the code either by allowing the empty label or by raising the propoer error.

Modifying the documentation does not seem sufficient from my point of view.

Thanks for your suggestion for creating a specific class.

Odile Lambert

Matthew Pava

unread,
Mar 14, 2019, 9:44:22 AM3/14/19
to Odile Lambert, django...@googlegroups.com

Well, it is raising an error.  It’s a Python error. Basically, you were passing a keyword argument (empty_label), and the class was passing the same keyword argument called empty_label. Expanded out, this is what was happening:

 

field = forms.ModelMultipleChoiceField( queryset = Imodelclass.objects.all(), empty_label='------------' )

= ModelMultipleChoiceField(queryset= Imodelclass.objects.all(), empty_label=None, empty_label='------------')

 

When you read the error, you see that is actually very clear in this context: You passed two arguments for the same keyword.

typeError " __init__() got multiple values for keyword argument 'empty_label'

 

I think it would be wise to mention this in the documentation; perhaps even providing a rationale why ModelMultipleChoiceField should not have an empty label. But if there is no rationale behind it, then the clear solution is to do exactly as you did: remove the empty_label keyword argument from the super call.

Reply all
Reply to author
Forward
0 new messages