Code

Ticket #5238: initial-default.diff

File initial-default.diff, 965 bytes (added by Kevin Menard, 7 years ago)
Line 
1Index: django/newforms/models.py
2===================================================================
3--- django/newforms/models.py   (revision 5993)
4+++ django/newforms/models.py   (working copy)
5@@ -77,6 +77,8 @@
6     determining the formfield for a given database field. It's a callable that
7     takes a database Field instance and returns a form Field instance.
8     """
9+    from django.db.models.fields import NOT_PROVIDED
10+   
11     opts = model._meta
12     field_list = []
13     for f in opts.fields + opts.many_to_many:
14@@ -86,6 +88,9 @@
15             continue
16         formfield = formfield_callback(f)
17         if formfield:
18+            # Set the initial form field value based on the model field's default value.
19+            if f.default != NOT_PROVIDED:
20+                formfield.initial = f.default
21             field_list.append((f.name, formfield))
22     base_fields = SortedDictFromList(field_list)
23     return type(opts.object_name + 'Form', (form,),