Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #11940 - ModelForm evaluates callable default values on form cl…

…ass creation

Thanks to Harm Geerts for the report and initial patch.



git-svn-id: http://code.djangoproject.com/svn/django/trunk@12721 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit bae921dcff699b827cd423f01ed125221b32fc9a 1 parent 5e3a2e2
Luke Plant authored
4  django/db/models/fields/__init__.py
@@ -445,9 +445,11 @@ def formfield(self, form_class=forms.CharField, **kwargs):
445 445
         "Returns a django.forms.Field instance for this database Field."
446 446
         defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text}
447 447
         if self.has_default():
448  
-            defaults['initial'] = self.get_default()
449 448
             if callable(self.default):
  449
+                defaults['initial'] = self.default
450 450
                 defaults['show_hidden_initial'] = True
  451
+            else:
  452
+                defaults['initial'] = self.get_default()
451 453
         if self.choices:
452 454
             # Fields with choices get special treatment.
453 455
             include_blank = self.blank or not (self.has_default() or 'initial' in kwargs)
2  tests/modeltests/model_formsets/models.py
@@ -959,7 +959,7 @@ def __unicode__(self):
959 959
 # when determine what extra forms have changed to save.
960 960
 
961 961
 >>> form = formset.forms[0] # this formset only has one form
962  
->>> now = form.fields['date_joined'].initial
  962
+>>> now = form.fields['date_joined'].initial()
963 963
 >>> print form.as_p()
964 964
 <p><label for="id_membership_set-0-date_joined">Date joined:</label> <input type="text" name="membership_set-0-date_joined" value="..." id="id_membership_set-0-date_joined" /><input type="hidden" name="initial-membership_set-0-date_joined" value="..." id="initial-membership_set-0-id_membership_set-0-date_joined" /></p>
965 965
 <p><label for="id_membership_set-0-karma">Karma:</label> <input type="text" name="membership_set-0-karma" id="id_membership_set-0-karma" /><input type="hidden" name="membership_set-0-person" value="1" id="id_membership_set-0-person" /><input type="hidden" name="membership_set-0-id" id="id_membership_set-0-id" /></p>
13  tests/regressiontests/forms/models.py
@@ -17,10 +17,17 @@
17 17
 class BoundaryModel(models.Model):
18 18
     positive_integer = models.PositiveIntegerField(null=True, blank=True)
19 19
 
  20
+callable_default_value = 0
  21
+def callable_default():
  22
+    global callable_default_value
  23
+    callable_default_value = callable_default_value + 1
  24
+    return callable_default_value
  25
+
20 26
 class Defaults(models.Model):
21 27
     name = models.CharField(max_length=255, default='class default value')
22 28
     def_date = models.DateField(default = datetime.date(1980, 1, 1))
23 29
     value = models.IntegerField(default=42)
  30
+    callable_default = models.IntegerField(default=callable_default)
24 31
 
25 32
 class ChoiceModel(models.Model):
26 33
     """For ModelChoiceField and ModelMultipleChoiceField tests."""
@@ -112,6 +119,10 @@ def test_choices_not_fetched_when_not_rendering(self):
112 119
 datetime.date(1980, 1, 1)
113 120
 >>> DefaultsForm().fields['value'].initial
114 121
 42
  122
+>>> r1 = DefaultsForm()['callable_default'].as_widget()
  123
+>>> r2 = DefaultsForm()['callable_default'].as_widget()
  124
+>>> r1 == r2
  125
+False
115 126
 
116 127
 In a ModelForm that is passed an instance, the initial values come from the
117 128
 instance's values, not the model's defaults.
@@ -129,7 +140,7 @@ def test_choices_not_fetched_when_not_rendering(self):
129 140
 ...     name = CharField(max_length=255)
130 141
 ...     class Meta:
131 142
 ...         model = Defaults
132  
-...         exclude = ['name']
  143
+...         exclude = ['name', 'callable_default']
133 144
 >>> f = ExcludingForm({'name': u'Hello', 'value': 99, 'def_date': datetime.date(1999, 3, 2)})
134 145
 >>> f.is_valid()
135 146
 True

0 notes on commit bae921d

Please sign in to comment.
Something went wrong with that request. Please try again.