Thursday, February 26, 2015

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

Hi,

Am 25.02.2015 um 19:11 schrieb Carsten Fuchs:
> the parent model's History view claims that field "monat" of TestModel has been changed,

I got at least a little bit further: This is easily reproducible in a
minimal project/app test case with SQLite exactly as described in
Django's intro tutorial 01. This is the entire contents of my test app's
`models.py` file:


# -*- coding: utf-8 -*-
from django.db import models

def VormonatsMonat():
return 1

MONTHS_CHOICES = (
(1, "Jan"),
(2, "Feb"),
)

class Choice(models.Model):
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
monat = models.IntegerField(default=VormonatsMonat,
choices=MONTHS_CHOICES)


Then, at the `./manage.py shell` prompt (creating a Choice instance is
omitted here):

>>> from django.forms import ModelForm
>>> from TestApp.models import Choice
>>> class ChoiceForm(ModelForm):
... class Meta:
... model = Choice
... fields = ["choice_text", "votes", "monat"]
...
>>> ch = Choice.objects.get(pk=1)
>>> chv = Choice.objects.values().get(pk=1)
>>> ch
<Choice: Choice object>
>>> chv
{'monat': 1, 'choice_text': u'just a test', 'votes': 0, u'id': 1}
>>> ChoiceForm(chv, initial=chv, instance=ch).has_changed()
True


The expected output is `False`, which is (in a new shell session) in
fact obtained whenever the definition of field `monat` is slightly
changed, e.g. to a constant default value rather than a callable, or
with no choices.

Any thoughts please?

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/54EF60B7.1010204%40cafu.de.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment