#24428: Model field with default=callable and choices always is always reported 
at
changed
-------------------------------+--------------------
     Reporter:  CarstenF       |      Owner:  nobody
         Type:  Bug            |     Status:  new
    Component:  Uncategorized  |    Version:  1.7
     Severity:  Normal         |   Keywords:
 Triage Stage:  Unreviewed     |  Has patch:  0
Easy pickings:  0              |      UI/UX:  0
-------------------------------+--------------------
 Originating thread at django-users:
 https://groups.google.com/forum/#!topic/django-users/WhuG7X9eRWk

 In a newly created Django project/app, please consider this `models.py`
 file:

 {{{
 #!python
 # -*- 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):

 {{{
 #!python
 >>> 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.

 I got as far as `BaseForm.changed_data()` in `django/forms/forms.py`,
 where for field `monat` we get
   - `field.show_hidden_initial == True`,
   - `hidden_widget.value_from_datadict(self.data, self.files,
 initial_prefixed_name) == None`,
   - resulting in `initial_value == u""`,
 resulting in the observed behaviour.

 Is maybe `field.show_hidden_initial == True` (wrongly) because the
 `default=callable` makes it seem that the default value is not one of the
 available choices?

--
Ticket URL: <https://code.djangoproject.com/ticket/24428>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/051.9c6df1834a633e13619e5f7dd01bb549%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to