[Django] #21224: TypedChoiceField does not properly coerce 'None' in certain circumstances

19 views
Skip to first unread message

Django

unread,
Oct 5, 2013, 2:54:07 AM10/5/13
to django-...@googlegroups.com
#21224: TypedChoiceField does not properly coerce 'None' in certain circumstances
----------------------------------------------+--------------------
Reporter: anonymous | Owner: nobody
Type: Uncategorized | Status: new
Component: Database layer (models, ORM) | Version: 1.5
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
----------------------------------------------+--------------------
{{{
class MyModelAdmin(admin.ModelAdmin):
def formfield_for_dbfield(self, db_field, **kwargs):
if db_field.name == 'integerfield':
return
forms.TypedChoiceField(choices=((None,'Undecided'),(-1, 'Reject'), (0,
'Neutral'), (1,'Accept')))
}}}

Selecting 'Undecided' here results in the error message "'None' must be an
integer"

In order to fix this I have to add the following param to
TypedChoiceField:

{{{coerce=lambda v: int(v) if v and v != 'None' else None}}}

If I try this instead it results in the error "Select a valid choice. None
is not one of the available choices.":

{{{coerce=lambda v: int(v) if v else None}}}

It appears that the None choice is getting coerced to a string before it
passes into TypedChoiceField.to_python (or wherever it gets passed to
first)?

--
Ticket URL: <https://code.djangoproject.com/ticket/21224>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Oct 16, 2013, 8:40:16 AM10/16/13
to django-...@googlegroups.com
#21224: TypedChoiceField does not properly coerce 'None' in certain circumstances
-------------------------------------+-------------------------------------

Reporter: anonymous | Owner: nobody
Type: Uncategorized | Status: new
Component: Database layer | Version: 1.5
(models, ORM) | Resolution:
Severity: Normal | Triage Stage:
Keywords: | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timo):

* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0


Comment:

This may be addressed in master (1.7) with #20649 - could you take a look
and see if that meets your use case?

--
Ticket URL: <https://code.djangoproject.com/ticket/21224#comment:1>

Django

unread,
Nov 7, 2013, 2:26:40 PM11/7/13
to django-...@googlegroups.com
#21224: TypedChoiceField does not properly coerce 'None' in certain circumstances
-------------------------------------+-------------------------------------
Reporter: anonymous | Owner: nobody
Type: Uncategorized | Status: closed

Component: Database layer | Version: 1.5
(models, ORM) | Resolution:
Severity: Normal | worksforme
Keywords: | Triage Stage:
Has patch: 0 | Unreviewed
Needs tests: 0 | Needs documentation: 0
Easy pickings: 0 | Patch needs improvement: 0
| UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by claudep):

* status: new => closed
* resolution: => worksforme


Comment:

That change:
https://github.com/django/django/commit/1123f4551158b7fc65d3bd88c375a4517dcd0720
#diff-aee6730bae795d31efec3c5d014804a9R511
...should solve your issue, as the `None` choice will be rendered as the
empty string in the option value, then coercion should consider it as the
empty value. Reopen if you can reproduce an issue with master code.

--
Ticket URL: <https://code.djangoproject.com/ticket/21224#comment:2>

Reply all
Reply to author
Forward
0 new messages