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
This commit is contained in:
parent
24b9c65d3f
commit
7be4b9a4c0
|
@ -149,7 +149,6 @@ def fields_for_model(model, fields=None, exclude=None, formfield_callback=lambda
|
||||||
fields will be excluded from the returned fields, even if they are listed
|
fields will be excluded from the returned fields, even if they are listed
|
||||||
in the ``fields`` argument.
|
in the ``fields`` argument.
|
||||||
"""
|
"""
|
||||||
# TODO: if fields is provided, it would be nice to return fields in that order
|
|
||||||
field_list = []
|
field_list = []
|
||||||
opts = model._meta
|
opts = model._meta
|
||||||
for f in opts.fields + opts.many_to_many:
|
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
|
||||||
formfield = formfield_callback(f)
|
formfield = formfield_callback(f)
|
||||||
if formfield:
|
if formfield:
|
||||||
field_list.append((f.name, formfield))
|
field_list.append((f.name, formfield))
|
||||||
return SortedDict(field_list)
|
field_dict = SortedDict(field_list)
|
||||||
|
if fields:
|
||||||
|
field_dict = SortedDict([(f, field_dict[f]) for f in fields if (not exclude) or (exclude and f not in exclude)])
|
||||||
|
return field_dict
|
||||||
|
|
||||||
class ModelFormOptions(object):
|
class ModelFormOptions(object):
|
||||||
def __init__(self, options=None):
|
def __init__(self, options=None):
|
||||||
|
|
|
@ -259,7 +259,8 @@ model fields:
|
||||||
|
|
||||||
2. Use the ``fields`` attribute of the ``ModelForm``'s inner ``Meta``
|
2. Use the ``fields`` attribute of the ``ModelForm``'s inner ``Meta``
|
||||||
class. This attribute, if given, should be a list of field names
|
class. This attribute, if given, should be a list of field names
|
||||||
to include in the form.
|
to include in the form. The form will render the fields in the same
|
||||||
|
order they are specified in the ``fields`` attribute.
|
||||||
|
|
||||||
3. Use the ``exclude`` attribute of the ``ModelForm``'s inner ``Meta``
|
3. Use the ``exclude`` attribute of the ``ModelForm``'s inner ``Meta``
|
||||||
class. This attribute, if given, should be a list of field names
|
class. This attribute, if given, should be a list of field names
|
||||||
|
@ -336,6 +337,31 @@ parameter when declaring the form field::
|
||||||
... class Meta:
|
... class Meta:
|
||||||
... model = Article
|
... model = Article
|
||||||
|
|
||||||
|
Changing the order of fields
|
||||||
|
----------------------------
|
||||||
|
|
||||||
|
By default, a ``ModelForm`` will render fields in the same order that
|
||||||
|
they are defined on the model, with ``ManyToManyField``s appearing last.
|
||||||
|
If you want to change the order in which fields are rendered, you can
|
||||||
|
use the ``fields`` attribute on the ``Meta`` class.
|
||||||
|
|
||||||
|
The ``fields`` attribute defines the subset of model fields that will be
|
||||||
|
rendered, and the order in which they will be rendered. For example given this
|
||||||
|
model::
|
||||||
|
|
||||||
|
class Book(models.Model):
|
||||||
|
author = models.ForeignKey(Author)
|
||||||
|
title = models.CharField(max_length=100)
|
||||||
|
|
||||||
|
the ``author`` field would be rendered first. If we wanted the title field
|
||||||
|
to be rendered first, we could specify the following ``ModelForm``::
|
||||||
|
|
||||||
|
>>> class BookForm(ModelForm):
|
||||||
|
... class Meta:
|
||||||
|
... model = Book
|
||||||
|
... fields = ['title', 'author']
|
||||||
|
|
||||||
|
|
||||||
Overriding the clean() method
|
Overriding the clean() method
|
||||||
-----------------------------
|
-----------------------------
|
||||||
|
|
||||||
|
|
|
@ -105,12 +105,12 @@ try:
|
||||||
# If PIL is not available, ImageField tests are omitted.
|
# If PIL is not available, ImageField tests are omitted.
|
||||||
from PIL import Image, _imaging
|
from PIL import Image, _imaging
|
||||||
test_images = True
|
test_images = True
|
||||||
|
|
||||||
class ImageFile(models.Model):
|
class ImageFile(models.Model):
|
||||||
def custom_upload_path(self, filename):
|
def custom_upload_path(self, filename):
|
||||||
path = self.path or 'tests'
|
path = self.path or 'tests'
|
||||||
return '%s/%s' % (path, filename)
|
return '%s/%s' % (path, filename)
|
||||||
|
|
||||||
description = models.CharField(max_length=20)
|
description = models.CharField(max_length=20)
|
||||||
image = models.ImageField(storage=temp_storage, upload_to=custom_upload_path,
|
image = models.ImageField(storage=temp_storage, upload_to=custom_upload_path,
|
||||||
width_field='width', height_field='height')
|
width_field='width', height_field='height')
|
||||||
|
@ -120,15 +120,15 @@ try:
|
||||||
|
|
||||||
def __unicode__(self):
|
def __unicode__(self):
|
||||||
return self.description
|
return self.description
|
||||||
|
|
||||||
class OptionalImageFile(models.Model):
|
class OptionalImageFile(models.Model):
|
||||||
def custom_upload_path(self, filename):
|
def custom_upload_path(self, filename):
|
||||||
path = self.path or 'tests'
|
path = self.path or 'tests'
|
||||||
return '%s/%s' % (path, filename)
|
return '%s/%s' % (path, filename)
|
||||||
|
|
||||||
description = models.CharField(max_length=20)
|
description = models.CharField(max_length=20)
|
||||||
image = models.ImageField(storage=temp_storage, upload_to=custom_upload_path,
|
image = models.ImageField(storage=temp_storage, upload_to=custom_upload_path,
|
||||||
width_field='width', height_field='height',
|
width_field='width', height_field='height',
|
||||||
blank=True, null=True)
|
blank=True, null=True)
|
||||||
width = models.IntegerField(editable=False, null=True)
|
width = models.IntegerField(editable=False, null=True)
|
||||||
height = models.IntegerField(editable=False, null=True)
|
height = models.IntegerField(editable=False, null=True)
|
||||||
|
@ -138,7 +138,7 @@ try:
|
||||||
return self.description
|
return self.description
|
||||||
except ImportError:
|
except ImportError:
|
||||||
test_images = False
|
test_images = False
|
||||||
|
|
||||||
class CommaSeparatedInteger(models.Model):
|
class CommaSeparatedInteger(models.Model):
|
||||||
field = models.CommaSeparatedIntegerField(max_length=20)
|
field = models.CommaSeparatedIntegerField(max_length=20)
|
||||||
|
|
||||||
|
@ -176,16 +176,16 @@ class Book(models.Model):
|
||||||
title = models.CharField(max_length=40)
|
title = models.CharField(max_length=40)
|
||||||
author = models.ForeignKey(Writer, blank=True, null=True)
|
author = models.ForeignKey(Writer, blank=True, null=True)
|
||||||
special_id = models.IntegerField(blank=True, null=True, unique=True)
|
special_id = models.IntegerField(blank=True, null=True, unique=True)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
unique_together = ('title', 'author')
|
unique_together = ('title', 'author')
|
||||||
|
|
||||||
class ExplicitPK(models.Model):
|
class ExplicitPK(models.Model):
|
||||||
key = models.CharField(max_length=20, primary_key=True)
|
key = models.CharField(max_length=20, primary_key=True)
|
||||||
desc = models.CharField(max_length=20, blank=True, unique=True)
|
desc = models.CharField(max_length=20, blank=True, unique=True)
|
||||||
class Meta:
|
class Meta:
|
||||||
unique_together = ('key', 'desc')
|
unique_together = ('key', 'desc')
|
||||||
|
|
||||||
def __unicode__(self):
|
def __unicode__(self):
|
||||||
return self.key
|
return self.key
|
||||||
|
|
||||||
|
@ -331,6 +331,29 @@ We can also subclass the Meta inner class to change the fields list.
|
||||||
<tr><th><label for="id_slug">Slug:</label></th><td><input id="id_slug" type="text" name="slug" maxlength="20" /></td></tr>
|
<tr><th><label for="id_slug">Slug:</label></th><td><input id="id_slug" type="text" name="slug" maxlength="20" /></td></tr>
|
||||||
<tr><th><label for="id_checkbox">Checkbox:</label></th><td><input type="checkbox" name="checkbox" id="id_checkbox" /></td></tr>
|
<tr><th><label for="id_checkbox">Checkbox:</label></th><td><input type="checkbox" name="checkbox" id="id_checkbox" /></td></tr>
|
||||||
|
|
||||||
|
# test using fields to provide ordering to the fields
|
||||||
|
>>> class CategoryForm(ModelForm):
|
||||||
|
... class Meta:
|
||||||
|
... model = Category
|
||||||
|
... fields = ['url', 'name']
|
||||||
|
|
||||||
|
>>> CategoryForm.base_fields.keys()
|
||||||
|
['url', 'name']
|
||||||
|
|
||||||
|
|
||||||
|
>>> print CategoryForm()
|
||||||
|
<tr><th><label for="id_url">The URL:</label></th><td><input id="id_url" type="text" name="url" maxlength="40" /></td></tr>
|
||||||
|
<tr><th><label for="id_name">Name:</label></th><td><input id="id_name" type="text" name="name" maxlength="20" /></td></tr>
|
||||||
|
|
||||||
|
>>> class CategoryForm(ModelForm):
|
||||||
|
... class Meta:
|
||||||
|
... model = Category
|
||||||
|
... fields = ['slug', 'url', 'name']
|
||||||
|
... exclude = ['url']
|
||||||
|
|
||||||
|
>>> CategoryForm.base_fields.keys()
|
||||||
|
['slug', 'name']
|
||||||
|
|
||||||
# Old form_for_x tests #######################################################
|
# Old form_for_x tests #######################################################
|
||||||
|
|
||||||
>>> from django.forms import ModelForm, CharField
|
>>> from django.forms import ModelForm, CharField
|
||||||
|
@ -1331,8 +1354,8 @@ False
|
||||||
True
|
True
|
||||||
|
|
||||||
# Unique & unique together with null values
|
# Unique & unique together with null values
|
||||||
>>> class BookForm(ModelForm):
|
>>> class BookForm(ModelForm):
|
||||||
... class Meta:
|
... class Meta:
|
||||||
... model = Book
|
... model = Book
|
||||||
>>> w = Writer.objects.get(name='Mike Royko')
|
>>> w = Writer.objects.get(name='Mike Royko')
|
||||||
>>> form = BookForm({'title': 'I May Be Wrong But I Doubt It', 'author' : w.pk})
|
>>> form = BookForm({'title': 'I May Be Wrong But I Doubt It', 'author' : w.pk})
|
||||||
|
|
Loading…
Reference in New Issue