Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #10512 -- Corrected the handling of extra fields on a ModelForm…

…. Thanks to Alex Gaynor for the patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@10070 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit cf7a3fa7f06814a7a22198fb9f84f6b9c0f09461 1 parent 536ccd1
Russell Keith-Magee authored March 17, 2009
2  django/forms/models.py
@@ -163,7 +163,7 @@ def fields_for_model(model, fields=None, exclude=None, formfield_callback=lambda
163 163
             field_list.append((f.name, formfield))
164 164
     field_dict = SortedDict(field_list)
165 165
     if fields:
166  
-        field_dict = SortedDict([(f, field_dict[f]) for f in fields if (not exclude) or (exclude and f not in exclude)])
  166
+        field_dict = SortedDict([(f, field_dict.get(f)) for f in fields if (not exclude) or (exclude and f not in exclude)])
167 167
     return field_dict
168 168
 
169 169
 class ModelFormOptions(object):
13  tests/modeltests/model_forms/models.py
@@ -1450,6 +1450,19 @@ def __unicode__(self):
1450 1450
 >>> core.parent
1451 1451
 <Inventory: Pear>
1452 1452
 
  1453
+>>> class CategoryForm(ModelForm):
  1454
+...     description = forms.CharField()
  1455
+...     class Meta:
  1456
+...         model = Category
  1457
+...         fields = ['description', 'url']
  1458
+
  1459
+>>> CategoryForm.base_fields.keys()
  1460
+['description', 'url']
  1461
+
  1462
+>>> print CategoryForm()
  1463
+<tr><th><label for="id_description">Description:</label></th><td><input type="text" name="description" id="id_description" /></td></tr>
  1464
+<tr><th><label for="id_url">The URL:</label></th><td><input id="id_url" type="text" name="url" maxlength="40" /></td></tr>
  1465
+
1453 1466
 # Clean up
1454 1467
 >>> import shutil
1455 1468
 >>> shutil.rmtree(temp_storage_dir)

0 notes on commit cf7a3fa

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