Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #899 -- Use model field default values as formfield initial val…

…ues in

form_for_model(). Patch from David Danier and PhiR. Thanks.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@6568 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit f20b254cccfecccaf81df8851790a0e033245d47 1 parent b7687fa
Malcolm Tredinnick authored October 20, 2007
2  django/db/models/fields/__init__.py
@@ -391,6 +391,8 @@ def formfield(self, form_class=forms.CharField, **kwargs):
391 391
         defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text}
392 392
         if self.choices:
393 393
             defaults['widget'] = forms.Select(choices=self.get_choices())
  394
+        if self.has_default():
  395
+            defaults['initial'] = self.get_default()
394 396
         defaults.update(kwargs)
395 397
         return form_class(**defaults)
396 398
 
50  tests/regressiontests/forms/models.py
... ...
@@ -1,21 +1,49 @@
  1
+import datetime
  2
+
1 3
 from django.db import models
2 4
 
3  
-class BoundaryModel(models.Model): 
  5
+class BoundaryModel(models.Model):
4 6
     positive_integer = models.PositiveIntegerField(null=True, blank=True)
5  
-    
  7
+
  8
+class 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
+
6 13
 __test__ = {'API_TESTS': """
7  
->>> from django.newforms import form_for_model
  14
+>>> from django.newforms import form_for_model, form_for_instance
8 15
 
9 16
 # 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()
13 20
 True
14  
->>> f = BoundaryForm({'positive_integer':0}) 
15  
->>> f.is_valid() 
  21
+>>> f = BoundaryForm({'positive_integer':0})
  22
+>>> f.is_valid()
16 23
 True
17  
->>> f = BoundaryForm({'positive_integer':-100}) 
18  
->>> f.is_valid() 
  24
+>>> f = BoundaryForm({'positive_integer':-100})
  25
+>>> f.is_valid()
19 26
 False
20 27
 
21  
-"""}
  28
+# Formfield initial values ########
  29
+If the model has default values for some fields, they are used as the formfield
  30
+initial values.
  31
+>>> DefaultsForm = form_for_model(Defaults)
  32
+>>> DefaultsForm().fields['name'].initial
  33
+u'class default value'
  34
+>>> DefaultsForm().fields['date'].initial
  35
+datetime.date(1980, 1, 1)
  36
+>>> DefaultsForm().fields['value'].initial
  37
+42
  38
+
  39
+In form_for_instance(), the initial values come from the instance's values, not
  40
+the 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
  44
+u'instance value'
  45
+>>> InstanceForm().fields['date'].initial
  46
+datetime.date(1969, 4, 4)
  47
+>>> InstanceForm().fields['value'].initial
  48
+12
  49
+"""}

0 notes on commit f20b254

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