Opened 5 years ago

Closed 5 years ago

#22097 closed Bug (fixed)

IntegerField with given choices leads to wrong has_changed() work

Reported by: igor.mitrenko@… Owned by: Claude Paroz
Component: Forms Version: 1.7-alpha-1
Severity: Release blocker Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


It seems that providing choices to IntegerField makes ModelForm (created by admin, for example) to use ChoiceField and call it's to_python method, which return string. Finally, _has_changed method compares initial value (of type int) to new value (of type unicode), and always returns True.
My code sending "object created" signals on each inline object change, so it leads to creation of unnecessary "changed" events logging.


class PhoneNumber(models.Model):
    Phone number set for workers
    WORK_PHONE = 1
    HOME_PHONE = 2
        (MOBILE_PHONE, _('Mobile')),
        (WORK_PHONE, _('Work')),
        (HOME_PHONE, pgettext('kind of phone number', 'Home')),
    worker = models.ForeignKey(Worker)
    phone_number = models.CharField(_('phone number'), max_length=12)
    kind = models.SmallIntegerField(_('kind of number'), choices=KIND_CHOICES, default=MOBILE_PHONE)

ModelForm derived from such a model created ChoiceField for kind, but it's has_changed method return True always, cause it's (as example) always 1 != u'1'

Removing choices argument is a workaround, but it breaks desired behaviour.
Another workaround is to use CharField, but the problem still remains.
Django version 1.7.dev20131210091409

Change History (2)

comment:1 Changed 5 years ago by Claude Paroz

Owner: changed from nobody to Claude Paroz
Severity: NormalRelease blocker
Status: newassigned
Triage Stage: UnreviewedAccepted

This regression was introduced in a0f3eeccf3d5a90f9914bfe20b15df05673ea59d in response to the use case described in #21397.

comment:2 Changed 5 years ago by Claude Paroz <claude@…>

Resolution: fixed
Status: assignedclosed

In cb844497d01ddb45603e47891cdf36ae0b006d03:

Fixed #22097 -- Fixed change detection for TypedChoiceField

Thanks Igor Mitrenko for the report.

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