#11940: ModelForm evaluates callable default values on form class creation
-------------------------------------------+--------------------------------
 Reporter:  Harm Geerts <[email protected]>  |       Owner:  nobody    
   Status:  new                            |   Milestone:            
Component:  Forms                          |     Version:  SVN       
 Keywords:                                 |       Stage:  Unreviewed
Has_patch:  1                              |  
-------------------------------------------+--------------------------------
 Using a ModelForm in combination with a field that defines a callable
 default value will cause the callable to be evaluated on form class
 creation.

 {{{
 class TestModel(models.Model):
     datetime = models.DateTimeField(default=datetime.datetime.now)

 class TestModelForm(forms.ModelForm):
     class Meta:
         model = TestModel
 }}}

 {{{
 >>> form = TestModelForm()
 >>> print form['datetime']
 <input type="text" name="datetime" value="2009-09-23 16:49:34"
 id="id_datetime" /><input type="hidden" name="initial-datetime"
 value="2009-09-23 16:49:34.561971" id="id_datetime" />
 >>> # wait a bit
 >>> form = TestModelForm()
 >>> print form['datetime']
 <input type="text" name="datetime" value="2009-09-23 16:49:34"
 id="id_datetime" /><input type="hidden" name="initial-datetime"
 value="2009-09-23 16:49:34.561971" id="id_datetime" />
 }}}

 The attached patch will fix this and preserve the callable default. It
 also implements Field.initial as a property that will evaluate callables
 when called.
 I'm not sure this is desired but I found a case in the modeltests that
 depended on initial having a real value and I can imagine there is more
 code out there that expects this behaviour (in fact my regression test now
 depends on it). I'll leave it up to you to decide what to do with it.

 The test case that depends on initial having a real value is in
 modeltests/model_formsets/models.py:886

-- 
Ticket URL: <http://code.djangoproject.com/ticket/11940>
Django <http://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--~--~---------~--~----~------------~-------~--~----~
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