Ticket #899: 899.diff

File 899.diff, 2.0 KB (added by Philippe Raoult, 17 years ago)

same patch against trunk, but with the tests requested by Malcolm

  • django/db/models/fields/__init__.py

     
    391391        defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text}
    392392        if self.choices:
    393393            defaults['widget'] = forms.Select(choices=self.get_choices())
     394        if self.has_default():
     395            defaults['initial'] = self.get_default()
    394396        defaults.update(kwargs)
    395397        return form_class(**defaults)
    396398
  • tests/regressiontests/forms/tests.py

     
    38583858
    38593859"""
    38603860
     3861from django.db import models
     3862import datetime
     3863
     3864class Foo(models.Model):
     3865    name = models.CharField(max_length=256, default='class wide default value')
     3866    date = models.DateField(default = datetime.date(1980, 1, 1))
     3867    value = models.IntegerField(default=42)
     3868
     3869formfield_initial_from_default_tests = ur"""
     3870>>> from django import newforms as forms
     3871>>> FooForm = forms.form_for_model(Foo)
     3872>>> FooForm().fields['name'].initial
     3873'class wide default value'
     3874>>> FooForm().fields['date'].initial
     3875datetime.date(1980, 1, 1)
     3876>>> FooForm().fields['value'].initial
     387742
     3878>>> foo_instance = Foo(name='instance value', date = datetime.date(1969, 4, 4), value = 12)
     3879>>> InstanceForm = forms.form_for_instance(foo_instance)
     3880>>> InstanceForm().fields['name'].initial
     3881'instance value'
     3882>>> InstanceForm().fields['date'].initial
     3883datetime.date(1969, 4, 4)
     3884>>> InstanceForm().fields['value'].initial
     388512
     3886    """
     3887
    38613888__test__ = {
    38623889    'form_tests': form_tests,
    38633890    'localflavor': localflavor_tests,
    38643891    'regressions': regression_tests,
    38653892    'util_tests': util_tests,
     3893    'formfield_initial_from_default_tests' : formfield_initial_from_default_tests,
    38663894}
    38673895
    38683896if __name__ == "__main__":
Back to Top