Nullable ChoiceField on ModelForm - has_changed always True

42 views
Skip to first unread message

steve byerly

unread,
Jan 19, 2016, 7:23:00 PM1/19/16
to Django users
I have a model with a nullable field (PositiveSmallIntegerField) with defined choices. When I make a ModelForm for the model and do not select a value for this field, the field always comes back in the changed data. The reason is that the empty choice value, the data_value, is an empty string, while the initial_value is None.

I can override the base field and make a TypedChoiceField, and provide a function to coerce which returns None if the value is an empty string.

Is this the only route, or is there a better way to accomplish this?

steve byerly

unread,
Jan 19, 2016, 7:45:52 PM1/19/16
to Django users
Also of note, I recently upgraded to Django 1.9 from Django 1.7 where this was working fine. It looks like this commit is the cause of my problem:

steve byerly

unread,
Jan 20, 2016, 5:29:45 PM1/20/16
to Django users
It actually looks like the commit fixed the regression and was in 1.9.1. Didn't realize it had been released.

James Schneider

unread,
Jan 20, 2016, 5:36:12 PM1/20/16
to django...@googlegroups.com
On Tue, Jan 19, 2016 at 4:45 PM, steve byerly <sbye...@gmail.com> wrote:
Also of note, I recently upgraded to Django 1.9 from Django 1.7 where this was working fine. It looks like this commit is the cause of my problem:

Is there any mention of a behavior change in the release notes between those versions? If you can come up with a simple example case, I would recommend filing a ticket about the behavior change, or posting this to the django-dev mailing list. 

Looks like this ticket is related: https://code.djangoproject.com/ticket/25942 as was noted in the diff you linked above.

-James
Reply all
Reply to author
Forward
0 new messages