Wednesday, February 25, 2015

Model field with default value and choices is always reported as changed, even if unchanged

Hi all,

please consider this model definition:


def PrevMonth():
h = date.today()
return 12 if h.month == 1 else h.month-1

MONTHS_CHOICES = (
(1, "Januar"), (2, "Februar"), (3, "März"), (4, "April"),
(5, "Mai"), (6, "Juni"), (7, "Juli"), (8, "August"),
(9, "September"), (10, "Oktober"), (11, "November"), (12, "Dezember")
)

class TestModel(models.Model):
key = models.ForeignKey(ParentModel, db_column='key', to_field='key')
monat = models.SmallIntegerField(default=PrevMonth,
choices=MONTHS_CHOICES)


I use TestModel in the Django Admin "inline", i.e. as the related model
of ParentModel.

The problem is that whenever an existing ParentModel instance with
existing TestModel inlines is saved, the parent model's History view
claims that field "monat" of TestModel has been changed, even if it
hasn't. (If many instances of TestModel refer to the same parent model
instance, the claim is made for each TestModel instance.)

The problem disappears if in the definition of field `monat`, either
parameter `default` or `choices` is omitted. It also disappears if I
change the definition to use a constant for the default value, e.g.
`default=6`.

Does someone know what may be causing this, or how (else) it can be avoided?

Best regards,
Carsten

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/54EE1059.4040901%40cafu.de.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment