From fdd5eb4309535bf49b3138e0954c6da553c1adef Mon Sep 17 00:00:00 2001 From: Mariusz Felisiak Date: Tue, 5 May 2020 09:08:29 +0200 Subject: [PATCH] [3.0.x] Fixed #31538 -- Fixed Meta.ordering validation lookups that are not transforms. Regression in 440505cb2cadbe1a5b9fba246bcde6c04f51d07e. Thanks Simon Meers for the report. Backport of b73e66e75802f10cc34d4880714554cea54dbf49 from master --- django/db/models/base.py | 4 +++- docs/releases/3.0.7.txt | 3 ++- tests/invalid_models_tests/test_models.py | 9 +++++++++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/django/db/models/base.py b/django/db/models/base.py index 3552dc5783b..63801da3c29 100644 --- a/django/db/models/base.py +++ b/django/db/models/base.py @@ -1719,7 +1719,9 @@ class Model(metaclass=ModelBase): else: _cls = None except (FieldDoesNotExist, AttributeError): - if fld is None or fld.get_transform(part) is None: + if fld is None or ( + fld.get_transform(part) is None and fld.get_lookup(part) is None + ): errors.append( checks.Error( "'ordering' refers to the nonexistent field, " diff --git a/docs/releases/3.0.7.txt b/docs/releases/3.0.7.txt index 6b204973bea..9fc71d9aa28 100644 --- a/docs/releases/3.0.7.txt +++ b/docs/releases/3.0.7.txt @@ -9,4 +9,5 @@ Django 3.0.7 fixes several bugs in 3.0.6. Bugfixes ======== -* ... +* Fixed a regression in Django 3.0 by restoring the ability to use field + lookups in ``Meta.ordering`` (:ticket:`31538`). diff --git a/tests/invalid_models_tests/test_models.py b/tests/invalid_models_tests/test_models.py index 60b89b6f2ec..8a204162d3d 100644 --- a/tests/invalid_models_tests/test_models.py +++ b/tests/invalid_models_tests/test_models.py @@ -844,6 +844,15 @@ class OtherModelTests(SimpleTestCase): with register_lookup(models.CharField, Lower): self.assertEqual(Model.check(), []) + def test_ordering_pointing_to_lookup_not_transform(self): + class Model(models.Model): + test = models.CharField(max_length=100) + + class Meta: + ordering = ('test__isnull',) + + self.assertEqual(Model.check(), []) + def test_ordering_pointing_to_related_model_pk(self): class Parent(models.Model): pass