Opened 11 years ago

Closed 11 years ago

#6146 closed (invalid)

newsforms.widget.Select does not accept long int as choices

Reported by: Tim <timsloan@…> Owned by: nobody
Component: Forms Version: master
Severity: Keywords: choices mysql
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


While using newforms form_for_instance or form_for_model, I was trying to override the form:

MyModelAddForm = form_for_model(MyModel)
MyModelAddForm.base_fields['field_one'].widget = forms.Select(choices=[(, for testa in Test.objects.all()])
MyModelAddForm.base_fields['field_two'].widget = forms.CheckboxSelectMultiple(choices=[(, for testb in Test.objects.all()])

However, I was getting the error that there are "too many values to unpack". After a little debugging I found that MySQL is returning "1L", "2L", etc. as the id/pk. This is recognized in the tests as MySQL behavior. However, for new forms the Select field seems to be looking for an Integer only.

By adding int() around the returning id/pk field, the problem was fixed.

MyModelAddForm = form_for_model(MyModel)
MyModelAddForm.base_fields['field_one'].widget = forms.Select(choices=[(int(, for testa in Test.objects.all()])
MyModelAddForm.base_fields['field_two'].widget = forms.CheckboxSelectMultiple(choices=[(int(, for testb in Test.objects.all()])

I believe that Long Integers from MySQL shouldn't have the trailing L.

Change History (1)

comment:1 Changed 11 years ago by Philippe Raoult

Resolution: invalid
Status: newclosed
Summary: Integers (PK fields) on MySQL break newforms Choicesnewsforms.widget.Select does not accept long int as choices

Those are HTML widgets and as such only deal with strings. You really should be adding str() around testa/

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