Skip to content

Commit 7be4b9a

Browse files
committed
Fixed #8164 -- Fields on a ModelForm are now ordered in the order specified in the fields attribute of the ModelForm's Meta class. Thanks to Alex Gaynor for the patch.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@10062 bcc190cf-cafb-0310-a4f2-bffc1f526a37
1 parent 24b9c65 commit 7be4b9a

File tree

3 files changed

+65
-14
lines changed

3 files changed

+65
-14
lines changed

django/forms/models.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,6 @@ def fields_for_model(model, fields=None, exclude=None, formfield_callback=lambda
149149
fields will be excluded from the returned fields, even if they are listed
150150
in the ``fields`` argument.
151151
"""
152-
# TODO: if fields is provided, it would be nice to return fields in that order
153152
field_list = []
154153
opts = model._meta
155154
for f in opts.fields + opts.many_to_many:
@@ -162,7 +161,10 @@ def fields_for_model(model, fields=None, exclude=None, formfield_callback=lambda
162161
formfield = formfield_callback(f)
163162
if formfield:
164163
field_list.append((f.name, formfield))
165-
return SortedDict(field_list)
164+
field_dict = SortedDict(field_list)
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)])
167+
return field_dict
166168

167169
class ModelFormOptions(object):
168170
def __init__(self, options=None):

docs/topics/forms/modelforms.txt

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,8 @@ model fields:
259259

260260
2. Use the ``fields`` attribute of the ``ModelForm``'s inner ``Meta``
261261
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 form will render the fields in the same
263+
order they are specified in the ``fields`` attribute.
263264

264265
3. Use the ``exclude`` attribute of the ``ModelForm``'s inner ``Meta``
265266
class. This attribute, if given, should be a list of field names
@@ -336,6 +337,31 @@ parameter when declaring the form field::
336337
... class Meta:
337338
... model = Article
338339

340+
Changing the order of fields
341+
----------------------------
342+
343+
By default, a ``ModelForm`` will render fields in the same order that
344+
they are defined on the model, with ``ManyToManyField``s appearing last.
345+
If you want to change the order in which fields are rendered, you can
346+
use the ``fields`` attribute on the ``Meta`` class.
347+
348+
The ``fields`` attribute defines the subset of model fields that will be
349+
rendered, and the order in which they will be rendered. For example given this
350+
model::
351+
352+
class Book(models.Model):
353+
author = models.ForeignKey(Author)
354+
title = models.CharField(max_length=100)
355+
356+
the ``author`` field would be rendered first. If we wanted the title field
357+
to be rendered first, we could specify the following ``ModelForm``::
358+
359+
>>> class BookForm(ModelForm):
360+
... class Meta:
361+
... model = Book
362+
... fields = ['title', 'author']
363+
364+
339365
Overriding the clean() method
340366
-----------------------------
341367

tests/modeltests/model_forms/models.py

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,12 @@ def __unicode__(self):
105105
# If PIL is not available, ImageField tests are omitted.
106106
from PIL import Image, _imaging
107107
test_images = True
108-
108+
109109
class ImageFile(models.Model):
110110
def custom_upload_path(self, filename):
111111
path = self.path or 'tests'
112112
return '%s/%s' % (path, filename)
113-
113+
114114
description = models.CharField(max_length=20)
115115
image = models.ImageField(storage=temp_storage, upload_to=custom_upload_path,
116116
width_field='width', height_field='height')
@@ -120,15 +120,15 @@ def custom_upload_path(self, filename):
120120

121121
def __unicode__(self):
122122
return self.description
123-
123+
124124
class OptionalImageFile(models.Model):
125125
def custom_upload_path(self, filename):
126126
path = self.path or 'tests'
127127
return '%s/%s' % (path, filename)
128-
128+
129129
description = models.CharField(max_length=20)
130130
image = models.ImageField(storage=temp_storage, upload_to=custom_upload_path,
131-
width_field='width', height_field='height',
131+
width_field='width', height_field='height',
132132
blank=True, null=True)
133133
width = models.IntegerField(editable=False, null=True)
134134
height = models.IntegerField(editable=False, null=True)
@@ -138,7 +138,7 @@ def __unicode__(self):
138138
return self.description
139139
except ImportError:
140140
test_images = False
141-
141+
142142
class CommaSeparatedInteger(models.Model):
143143
field = models.CommaSeparatedIntegerField(max_length=20)
144144

@@ -176,16 +176,16 @@ class Book(models.Model):
176176
title = models.CharField(max_length=40)
177177
author = models.ForeignKey(Writer, blank=True, null=True)
178178
special_id = models.IntegerField(blank=True, null=True, unique=True)
179-
179+
180180
class Meta:
181181
unique_together = ('title', 'author')
182-
182+
183183
class ExplicitPK(models.Model):
184184
key = models.CharField(max_length=20, primary_key=True)
185185
desc = models.CharField(max_length=20, blank=True, unique=True)
186186
class Meta:
187187
unique_together = ('key', 'desc')
188-
188+
189189
def __unicode__(self):
190190
return self.key
191191

@@ -331,6 +331,29 @@ def __unicode__(self):
331331
<tr><th><label for="id_slug">Slug:</label></th><td><input id="id_slug" type="text" name="slug" maxlength="20" /></td></tr>
332332
<tr><th><label for="id_checkbox">Checkbox:</label></th><td><input type="checkbox" name="checkbox" id="id_checkbox" /></td></tr>
333333
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+
344+
>>> print CategoryForm()
345+
<tr><th><label for="id_url">The URL:</label></th><td><input id="id_url" type="text" name="url" maxlength="40" /></td></tr>
346+
<tr><th><label for="id_name">Name:</label></th><td><input id="id_name" type="text" name="name" maxlength="20" /></td></tr>
347+
348+
>>> class CategoryForm(ModelForm):
349+
... class Meta:
350+
... model = Category
351+
... fields = ['slug', 'url', 'name']
352+
... exclude = ['url']
353+
354+
>>> CategoryForm.base_fields.keys()
355+
['slug', 'name']
356+
334357
# Old form_for_x tests #######################################################
335358
336359
>>> from django.forms import ModelForm, CharField
@@ -1331,8 +1354,8 @@ def __unicode__(self):
13311354
True
13321355
13331356
# Unique & unique together with null values
1334-
>>> class BookForm(ModelForm):
1335-
... class Meta:
1357+
>>> class BookForm(ModelForm):
1358+
... class Meta:
13361359
... model = Book
13371360
>>> w = Writer.objects.get(name='Mike Royko')
13381361
>>> form = BookForm({'title': 'I May Be Wrong But I Doubt It', 'author' : w.pk})

0 commit comments

Comments
 (0)