Ticket #11940: modelform-callable-default.patch

File modelform-callable-default.patch, 3.1 KB (added by Harm Geerts <hgeerts@…>, 15 years ago)
  • django/db/models/fields/__init__.py

     
    313313        "Returns a django.forms.Field instance for this database Field."
    314314        defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text}
    315315        if self.has_default():
    316             defaults['initial'] = self.get_default()
    317316            if callable(self.default):
     317                defaults['initial'] = self.default
    318318                defaults['show_hidden_initial'] = True
     319            else:
     320                defaults['initial'] = self.get_default()
    319321        if self.choices:
    320322            # Fields with choices get special treatment.
    321323            include_blank = self.blank or not (self.has_default() or 'initial' in kwargs)
  • django/forms/fields.py

     
    107107        messages.update(error_messages or {})
    108108        self.error_messages = messages
    109109
     110    def _get_initial(self):
     111        if callable(self._initial):
     112            return self._initial()
     113        return self._initial
     114
     115    def _set_initial(self, value):
     116        self._initial = value
     117
     118    initial = property(_get_initial, _set_initial)
     119
    110120    def clean(self, value):
    111121        """
    112122        Validates the given value and returns its "cleaned" value as an
  • tests/regressiontests/forms/models.py

     
    1515class BoundaryModel(models.Model):
    1616    positive_integer = models.PositiveIntegerField(null=True, blank=True)
    1717
     18callable_default_value = 1
     19def callable_default():
     20    global callable_default_value
     21    callable_default_value = callable_default_value + 1
     22    return callable_default_value
     23
    1824class Defaults(models.Model):
    1925    name = models.CharField(max_length=255, default='class default value')
    2026    def_date = models.DateField(default = datetime.date(1980, 1, 1))
    2127    value = models.IntegerField(default=42)
     28    callable_default = models.IntegerField(default=callable_default)
    2229
    2330class ChoiceModel(models.Model):
    2431    """For ModelChoiceField and ModelMultipleChoiceField tests."""
     
    8592datetime.date(1980, 1, 1)
    8693>>> DefaultsForm().fields['value'].initial
    879442
     95>>> DefaultsForm().fields['callable_default'].initial != DefaultsForm().fields['callable_default'].initial
     96True
    8897
    8998In a ModelForm that is passed an instance, the initial values come from the
    9099instance's values, not the model's defaults.
     
    102111...     name = CharField(max_length=255)
    103112...     class Meta:
    104113...         model = Defaults
    105 ...         exclude = ['name']
     114...         exclude = ['name', 'callable_default']
    106115>>> f = ExcludingForm({'name': u'Hello', 'value': 99, 'def_date': datetime.date(1999, 3, 2)})
    107116>>> f.is_valid()
    108117True
Back to Top