Nullable ChoiceField on ModelForm - has_changed always True
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? -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/1e11369b-7bcb-432f-acb6-c3785328b642%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: Nullable ChoiceField on ModelForm - has_changed always True
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: https://github.com/django/django/commit/ff077cd6496b6f82195e2dc040f70e19e7c206c9 On Tuesday, January 19, 2016 at 4:23:00 PM UTC-8, steve byerly wrote: > > 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? > -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/6c38874b-b876-43a5-827b-fcf8d48365cf%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: Nullable ChoiceField on ModelForm - has_changed always True
It actually looks like the commit fixed the regression and was in 1.9.1. Didn't realize it had been released. On Tuesday, January 19, 2016 at 4:45:52 PM UTC-8, steve byerly 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: > > https://github.com/django/django/commit/ff077cd6496b6f82195e2dc040f70e19e7c206c9 > > On Tuesday, January 19, 2016 at 4:23:00 PM UTC-8, steve byerly wrote: >> >> 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? >> > -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/e420f6da-86d2-42af-92ca-8b297d941a94%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.