Ticket #10512: modelforms-fields-order.diff

File modelforms-fields-order.diff, 1.6 KB (added by Alex Gaynor, 15 years ago)
  • django/forms/models.py

    diff --git a/django/forms/models.py b/django/forms/models.py
    index 595b775..d62a2ce 100644
    a b def fields_for_model(model, fields=None, exclude=None, formfield_callback=lambda  
    163163            field_list.append((f.name, formfield))
    164164    field_dict = SortedDict(field_list)
    165165    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)])
    167167    return field_dict
    168168
    169169class ModelFormOptions(object):
  • tests/modeltests/model_forms/models.py

    diff --git a/tests/modeltests/model_forms/models.py b/tests/modeltests/model_forms/models.py
    index 9261900..4075250 100644
    a b ValidationError: [u'Select a valid choice. z is not one of the available choices  
    14501450>>> core.parent
    14511451<Inventory: Pear>
    14521452
     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
    14531466# Clean up
    14541467>>> import shutil
    14551468>>> shutil.rmtree(temp_storage_dir)
Back to Top