Author: mtredinnick
Date: 2007-10-20 08:01:40 -0500 (Sat, 20 Oct 2007)
New Revision: 6568

Modified:
   django/trunk/django/db/models/fields/__init__.py
   django/trunk/tests/regressiontests/forms/models.py
Log:
Fixed #899 -- Use model field default values as formfield initial values in
form_for_model(). Patch from David Danier and PhiR. Thanks.


Modified: django/trunk/django/db/models/fields/__init__.py
===================================================================
--- django/trunk/django/db/models/fields/__init__.py    2007-10-20 13:01:09 UTC 
(rev 6567)
+++ django/trunk/django/db/models/fields/__init__.py    2007-10-20 13:01:40 UTC 
(rev 6568)
@@ -391,6 +391,8 @@
         defaults = {'required': not self.blank, 'label': 
capfirst(self.verbose_name), 'help_text': self.help_text}
         if self.choices:
             defaults['widget'] = forms.Select(choices=self.get_choices())
+        if self.has_default():
+            defaults['initial'] = self.get_default()
         defaults.update(kwargs)
         return form_class(**defaults)
 

Modified: django/trunk/tests/regressiontests/forms/models.py
===================================================================
--- django/trunk/tests/regressiontests/forms/models.py  2007-10-20 13:01:09 UTC 
(rev 6567)
+++ django/trunk/tests/regressiontests/forms/models.py  2007-10-20 13:01:40 UTC 
(rev 6568)
@@ -1,21 +1,49 @@
+import datetime
+
 from django.db import models
 
-class BoundaryModel(models.Model): 
+class BoundaryModel(models.Model):
     positive_integer = models.PositiveIntegerField(null=True, blank=True)
-    
+
+class Defaults(models.Model):
+    name = models.CharField(max_length=256, default='class default value')
+    date = models.DateField(default = datetime.date(1980, 1, 1))
+    value = models.IntegerField(default=42)
+
 __test__ = {'API_TESTS': """
->>> from django.newforms import form_for_model
+>>> from django.newforms import form_for_model, form_for_instance
 
 # Boundary conditions on a PostitiveIntegerField #########################
->>> BoundaryForm = form_for_model(BoundaryModel) 
->>> f = BoundaryForm({'positive_integer':100}) 
->>> f.is_valid() 
+>>> BoundaryForm = form_for_model(BoundaryModel)
+>>> f = BoundaryForm({'positive_integer':100})
+>>> f.is_valid()
 True
->>> f = BoundaryForm({'positive_integer':0}) 
->>> f.is_valid() 
+>>> f = BoundaryForm({'positive_integer':0})
+>>> f.is_valid()
 True
->>> f = BoundaryForm({'positive_integer':-100}) 
->>> f.is_valid() 
+>>> f = BoundaryForm({'positive_integer':-100})
+>>> f.is_valid()
 False
 
-"""}
\ No newline at end of file
+# Formfield initial values ########
+If the model has default values for some fields, they are used as the formfield
+initial values.
+>>> DefaultsForm = form_for_model(Defaults)
+>>> DefaultsForm().fields['name'].initial
+u'class default value'
+>>> DefaultsForm().fields['date'].initial
+datetime.date(1980, 1, 1)
+>>> DefaultsForm().fields['value'].initial
+42
+
+In form_for_instance(), the initial values come from the instance's values, not
+the model's defaults.
+>>> foo_instance = Defaults(name=u'instance value', date = datetime.date(1969, 
4, 4), value = 12)
+>>> InstanceForm = form_for_instance(foo_instance)
+>>> InstanceForm().fields['name'].initial
+u'instance value'
+>>> InstanceForm().fields['date'].initial
+datetime.date(1969, 4, 4)
+>>> InstanceForm().fields['value'].initial
+12
+"""}


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to