diff --git a/django/forms/models.py b/django/forms/models.py
index e6bbb98..d5eab82 100644
|
a
|
b
|
def fields_for_model(model, fields=None, exclude=None, formfield_callback=lambda
|
| 149 | 149 | fields will be excluded from the returned fields, even if they are listed |
| 150 | 150 | in the ``fields`` argument. |
| 151 | 151 | """ |
| 152 | | # TODO: if fields is provided, it would be nice to return fields in that order |
| 153 | 152 | field_list = [] |
| 154 | 153 | opts = model._meta |
| 155 | 154 | for f in opts.fields + opts.many_to_many: |
| … |
… |
def fields_for_model(model, fields=None, exclude=None, formfield_callback=lambda
|
| 162 | 161 | formfield = formfield_callback(f) |
| 163 | 162 | if formfield: |
| 164 | 163 | field_list.append((f.name, formfield)) |
| 165 | | return SortedDict(field_list) |
| | 164 | field_dict = SortedDict(field_list) |
| | 165 | if fields: |
| | 166 | field_dict.keyOrder = [f for f in fields if (not exclude) or (exclude and f not in exclude)] |
| | 167 | return field_dict |
| 166 | 168 | |
| 167 | 169 | class ModelFormOptions(object): |
| 168 | 170 | def __init__(self, options=None): |
diff --git a/docs/topics/forms/modelforms.txt b/docs/topics/forms/modelforms.txt
index 50beea2..fe1e2a1 100644
|
a
|
b
|
model fields:
|
| 259 | 259 | |
| 260 | 260 | 2. Use the ``fields`` attribute of the ``ModelForm``'s inner ``Meta`` |
| 261 | 261 | class. This attribute, if given, should be a list of field names |
| 262 | | to include in the form. |
| | 262 | to include in the form. The order of the items in ``fields`` will be the |
| | 263 | order the field's appear in when rendered if ``fields`` is provided. |
| 263 | 264 | |
| 264 | 265 | 3. Use the ``exclude`` attribute of the ``ModelForm``'s inner ``Meta`` |
| 265 | 266 | class. This attribute, if given, should be a list of field names |
diff --git a/tests/modeltests/model_forms/models.py b/tests/modeltests/model_forms/models.py
index e12363c..613b075 100644
|
a
|
b
|
We can also subclass the Meta inner class to change the fields list.
|
| 331 | 331 | <tr><th><label for="id_slug">Slug:</label></th><td><input id="id_slug" type="text" name="slug" maxlength="20" /></td></tr> |
| 332 | 332 | <tr><th><label for="id_checkbox">Checkbox:</label></th><td><input type="checkbox" name="checkbox" id="id_checkbox" /></td></tr> |
| 333 | 333 | |
| | 334 | # test using fields to provide ordering to the fields |
| | 335 | >>> class CategoryForm(ModelForm): |
| | 336 | ... class Meta: |
| | 337 | ... model = Category |
| | 338 | ... fields = ['url', 'name'] |
| | 339 | |
| | 340 | >>> CategoryForm.base_fields.keys() |
| | 341 | ['url', 'name'] |
| | 342 | |
| | 343 | >>> print CategoryForm() |
| | 344 | <tr><th><label for="id_url">The URL:</label></th><td><input id="id_url" type="text" name="url" maxlength="40" /></td></tr> |
| | 345 | <tr><th><label for="id_name">Name:</label></th><td><input id="id_name" type="text" name="name" maxlength="20" /></td></tr> |
| | 346 | |
| 334 | 347 | # Old form_for_x tests ####################################################### |
| 335 | 348 | |
| 336 | 349 | >>> from django.forms import ModelForm, CharField |