Code

Ticket #899: django-use-model-default-as-newforms-initial.diff

File django-use-model-default-as-newforms-initial.diff, 1.1 KB (added by David Danier <goliath.mailinglist@…>, 7 years ago)

This should do it for current trunk (formfield() nicely refactored using super())

Line 
1==== Patch <django-use-model-default-as-newforms-initial> level 1
2Source: f4626db1-382e-0410-8594-a82d82b39f71:/use-model-default-as-newforms-initial/trunk:5026 [local]
3Target: bcc190cf-cafb-0310-a4f2-bffc1f526a37:/django/trunk:5133 [mirrored]
4        (http://code.djangoproject.com/svn/django)
5Log:
6 r4878@damasonium:  ddanier | 2007-04-16 14:28:02 +0200
7  * Make model-field-default the initial value for newforms-fields
8
9=== django/db/models/fields/__init__.py
10==================================================================
11--- django/db/models/fields/__init__.py (revision 5133)
12+++ django/db/models/fields/__init__.py (patch django-use-model-default-as-newforms-initial level 1)
13@@ -346,6 +346,8 @@
14         defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text}
15         if self.choices:
16             defaults['widget'] = forms.Select(choices=self.get_choices())
17+        if self.has_default():
18+            defaults['initial'] = self.get_default()
19         defaults.update(kwargs)
20         return form_class(**defaults)
21