Opened 16 years ago

Closed 16 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: dev
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

Description

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=[(testa.id, testa.name) for testa in Test.objects.all()])
MyModelAddForm.base_fields['field_two'].widget = forms.CheckboxSelectMultiple(choices=[(testb.id, testb.name) 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 http://code.djangoproject.com/browser/django/trunk/tests/modeltests/field_defaults/models.py 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(testa.id), testa.name) for testa in Test.objects.all()])
MyModelAddForm.base_fields['field_two'].widget = forms.CheckboxSelectMultiple(choices=[(int(testb.id), testb.name) for testb in Test.objects.all()])

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

Change History (1)

comment:1 by Philippe Raoult, 16 years ago

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/testb.id.

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