diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py index 9fe6dd3c54..898f73e614 100644 --- a/django/db/models/fields/__init__.py +++ b/django/db/models/fields/__init__.py @@ -445,9 +445,11 @@ class Field(object): "Returns a django.forms.Field instance for this database Field." defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text} if self.has_default(): - defaults['initial'] = self.get_default() if callable(self.default): + defaults['initial'] = self.default defaults['show_hidden_initial'] = True + else: + defaults['initial'] = self.get_default() if self.choices: # Fields with choices get special treatment. include_blank = self.blank or not (self.has_default() or 'initial' in kwargs) diff --git a/tests/modeltests/model_formsets/models.py b/tests/modeltests/model_formsets/models.py index e8c3e98ac7..ba8592d909 100644 --- a/tests/modeltests/model_formsets/models.py +++ b/tests/modeltests/model_formsets/models.py @@ -959,7 +959,7 @@ False # when determine what extra forms have changed to save. >>> form = formset.forms[0] # this formset only has one form ->>> now = form.fields['date_joined'].initial +>>> now = form.fields['date_joined'].initial() >>> print form.as_p()

diff --git a/tests/regressiontests/forms/models.py b/tests/regressiontests/forms/models.py index 2127d40d95..229c50556e 100644 --- a/tests/regressiontests/forms/models.py +++ b/tests/regressiontests/forms/models.py @@ -17,10 +17,17 @@ temp_storage = FileSystemStorage(location=temp_storage_location) class BoundaryModel(models.Model): positive_integer = models.PositiveIntegerField(null=True, blank=True) +callable_default_value = 0 +def callable_default(): + global callable_default_value + callable_default_value = callable_default_value + 1 + return callable_default_value + class Defaults(models.Model): name = models.CharField(max_length=255, default='class default value') def_date = models.DateField(default = datetime.date(1980, 1, 1)) value = models.IntegerField(default=42) + callable_default = models.IntegerField(default=callable_default) class ChoiceModel(models.Model): """For ModelChoiceField and ModelMultipleChoiceField tests.""" @@ -112,6 +119,10 @@ u'class default value' datetime.date(1980, 1, 1) >>> DefaultsForm().fields['value'].initial 42 +>>> r1 = DefaultsForm()['callable_default'].as_widget() +>>> r2 = DefaultsForm()['callable_default'].as_widget() +>>> r1 == r2 +False In a ModelForm that is passed an instance, the initial values come from the instance's values, not the model's defaults. @@ -129,7 +140,7 @@ datetime.date(1969, 4, 4) ... name = CharField(max_length=255) ... class Meta: ... model = Defaults -... exclude = ['name'] +... exclude = ['name', 'callable_default'] >>> f = ExcludingForm({'name': u'Hello', 'value': 99, 'def_date': datetime.date(1999, 3, 2)}) >>> f.is_valid() True