Code

Opened 6 years ago

Closed 6 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: UI/UX:

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.

Attachments (0)

Change History (1)

comment:1 Changed 6 years ago by PhiR

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to invalid
  • Status changed from new to closed
  • Summary changed from Integers (PK fields) on MySQL break newforms Choices to newsforms.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.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.