Opened 6 years ago

Closed 6 years ago

#29742 closed Bug (needsinfo)

Problem overriding the FilteredSelectMultiple widget

Reported by: Bryant Glisson Owned by: nobody
Component: Forms Version: 1.11
Severity: Normal Keywords: forms widgets multiple select
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Bryant Glisson)

We have overridden the FilteredSelectMultiple widget here.
https://github.com/django/django/blob/201017df308266c7d5ed20181e6d0ffa5832e3e9/django/contrib/admin/widgets.py

We also override the form in which the widget is used in order to pass custom choices to it, using the following.

CHOICES = [(1, "One"), (2, "Two"), (3, "Three")]]
self.fields['topics'].widget = AdminTagsWidget('Topics', False, choices=CHOICES)

When we load the form, we get the following error:

TypeError at /site-admin/media/mediaitem/302444/change/
int() argument must be a string, a bytes-like object or a number, not 'list'

After much tinkering, I discovered that "self.is_stacked" is being filled with "self.choices" and is being used to populate the choices for the widget. To make things work. I overrode the "get_context" method as follows.

class AdminTagsWidget(FilteredSelectMultiple):
    def get_context(self, name, value, attrs):
        self.choices = self.is_stacked
        self.is_stacked = False
        return super().get_context(name, value, attrs)

Somewhere things are getting switched up when choices are passed into the widget. I don't know if this is the same for Django 2.0 or not. We are presently upgrading to python 3, and that is when the problem surfaced.

Change History (2)

comment:1 by Bryant Glisson, 6 years ago

Description: modified (diff)

comment:2 by Simon Charette, 6 years ago

Resolution: needsinfo
Status: newclosed

Hello there,

I'm afraid that there isn't enough to details to confirm Django is at fault here; the FilteredSelectMultiple widget should work just fine when initialized properly.

Did you override FilteredSelectMultiple.__init__ in your AdminTagsWidget class by any chance? Also, could you provide the full traceback of the exception you encounter?

Note: See TracTickets for help on using tickets.
Back to Top