From 5317864e5fda896e777cfd001ec40a3c924b853a Mon Sep 17 00:00:00 2001 From: Brian Rosner Date: Wed, 16 Jul 2008 01:02:57 +0000 Subject: [PATCH] newforms-admin: Fixed #7771 -- Improved the validation check on the ordering field. Now takes '?' and 'field1__field2' syntax into consideration. Thanks Michael Jung for catching this. git-svn-id: http://code.djangoproject.com/svn/django/branches/newforms-admin@7931 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/contrib/admin/validation.py | 6 ++++++ tests/regressiontests/modeladmin/models.py | 9 +++++++++ 2 files changed, 15 insertions(+) diff --git a/django/contrib/admin/validation.py b/django/contrib/admin/validation.py index 1173098668..fbf7de02ed 100644 --- a/django/contrib/admin/validation.py +++ b/django/contrib/admin/validation.py @@ -79,8 +79,14 @@ def validate(cls, model): "ordering marker `?`, but contains other fields as " "well. Please either remove `?` or the other fields." % cls.__name__) + if field == '?': + continue if field.startswith('-'): field = field[1:] + # Skip ordering in the format field1__field2 (FIXME: checking + # this format would be nice, but it's a little fiddly). + if '__' in field: + continue _check_field_existsw('ordering[%d]' % idx, field) # list_select_related = False diff --git a/tests/regressiontests/modeladmin/models.py b/tests/regressiontests/modeladmin/models.py index f95fec80d0..39f8599a78 100644 --- a/tests/regressiontests/modeladmin/models.py +++ b/tests/regressiontests/modeladmin/models.py @@ -30,6 +30,7 @@ class ValidationTestModel(models.Model): state = models.CharField(max_length=2, choices=(("CO", "Colorado"), ("WA", "Washington"))) is_active = models.BooleanField() pub_date = models.DateTimeField() + band = models.ForeignKey(Band) class ValidationTestInlineModel(models.Model): parent = models.ForeignKey(ValidationTestModel) @@ -610,6 +611,14 @@ Traceback (most recent call last): ... ImproperlyConfigured: `ValidationTestModelAdmin.ordering` has the random ordering marker `?`, but contains other fields as well. Please either remove `?` or the other fields. +>>> class ValidationTestModelAdmin(ModelAdmin): +... ordering = ('?',) +>>> validate(ValidationTestModelAdmin, ValidationTestModel) + +>>> class ValidationTestModelAdmin(ModelAdmin): +... ordering = ('band__name',) +>>> validate(ValidationTestModelAdmin, ValidationTestModel) + >>> class ValidationTestModelAdmin(ModelAdmin): ... ordering = ('name',) >>> validate(ValidationTestModelAdmin, ValidationTestModel)