Django

Code

Changeset 6568

Show
Ignore:
Timestamp:
10/20/07 08:01:40 (1 year ago)
Author:
mtredinnick
Message:

Fixed #899 -- Use model field default values as formfield initial values in
form_for_model(). Patch from David Danier and PhiR. Thanks.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/db/models/fields/__init__.py

    r6453 r6568  
    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) 
  • django/trunk/tests/regressiontests/forms/models.py

    r6252 r6568  
     1import datetime 
     2 
    13from django.db import models 
    24 
    3 class BoundaryModel(models.Model):  
     5class BoundaryModel(models.Model): 
    46    positive_integer = models.PositiveIntegerField(null=True, blank=True) 
    5      
     7 
     8class Defaults(models.Model): 
     9    name = models.CharField(max_length=256, default='class default value') 
     10    date = models.DateField(default = datetime.date(1980, 1, 1)) 
     11    value = models.IntegerField(default=42) 
     12 
    613__test__ = {'API_TESTS': """ 
    7 >>> from django.newforms import form_for_model 
     14>>> from django.newforms import form_for_model, form_for_instance 
    815 
    916# Boundary conditions on a PostitiveIntegerField ######################### 
    10 >>> BoundaryForm = form_for_model(BoundaryModel)  
    11 >>> f = BoundaryForm({'positive_integer':100})  
    12 >>> f.is_valid()  
     17>>> BoundaryForm = form_for_model(BoundaryModel) 
     18>>> f = BoundaryForm({'positive_integer':100}) 
     19>>> f.is_valid() 
    1320True 
    14 >>> f = BoundaryForm({'positive_integer':0})  
    15 >>> f.is_valid()  
     21>>> f = BoundaryForm({'positive_integer':0}) 
     22>>> f.is_valid() 
    1623True 
    17 >>> f = BoundaryForm({'positive_integer':-100})  
    18 >>> f.is_valid()  
     24>>> f = BoundaryForm({'positive_integer':-100}) 
     25>>> f.is_valid() 
    1926False 
    2027 
     28# Formfield initial values ######## 
     29If the model has default values for some fields, they are used as the formfield 
     30initial values. 
     31>>> DefaultsForm = form_for_model(Defaults) 
     32>>> DefaultsForm().fields['name'].initial 
     33u'class default value' 
     34>>> DefaultsForm().fields['date'].initial 
     35datetime.date(1980, 1, 1) 
     36>>> DefaultsForm().fields['value'].initial 
     3742 
     38 
     39In form_for_instance(), the initial values come from the instance's values, not 
     40the model's defaults. 
     41>>> foo_instance = Defaults(name=u'instance value', date = datetime.date(1969, 4, 4), value = 12) 
     42>>> InstanceForm = form_for_instance(foo_instance) 
     43>>> InstanceForm().fields['name'].initial 
     44u'instance value' 
     45>>> InstanceForm().fields['date'].initial 
     46datetime.date(1969, 4, 4) 
     47>>> InstanceForm().fields['value'].initial 
     4812 
    2149"""}