summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMariusz Felisiak <felisiak.mariusz@gmail.com>2019-07-11 13:40:36 +0200
committerGitHub <noreply@github.com>2019-07-11 13:40:36 +0200
commit7a42cfcfdc94c1e7cd653f3140b9eb30492bae4f (patch)
tree6bf03d51b438e982dbf15a7de56afb016baa6b7f
parent8c5f9906c56ac72fc4f13218dd90bdf9bc8a248b (diff)
Refs #30557 -- Fixed crash of ordering by ptr fields when Meta.ordering contains F() expressions.
Thanks Can Sarıgöl for the report. Follow up to 8c5f9906c56ac72fc4f13218dd90bdf9bc8a248b.
-rw-r--r--django/db/models/sql/compiler.py2
-rw-r--r--tests/ordering/models.py2
-rw-r--r--tests/ordering/tests.py2
3 files changed, 4 insertions, 2 deletions
diff --git a/django/db/models/sql/compiler.py b/django/db/models/sql/compiler.py
index 3cab405d2d..52ea717ca6 100644
--- a/django/db/models/sql/compiler.py
+++ b/django/db/models/sql/compiler.py
@@ -722,6 +722,8 @@ class SQLCompiler:
results = []
for item in opts.ordering:
+ if hasattr(item, 'resolve_expression') and not isinstance(item, OrderBy):
+ item = item.desc() if descending else item.asc()
if isinstance(item, OrderBy):
results.append((item, False))
continue
diff --git a/tests/ordering/models.py b/tests/ordering/models.py
index 4a3e340e71..62df09351f 100644
--- a/tests/ordering/models.py
+++ b/tests/ordering/models.py
@@ -33,7 +33,7 @@ class Article(models.Model):
class Meta:
ordering = (
'-pub_date',
- 'headline',
+ models.F('headline'),
models.F('author__name').asc(),
OrderBy(models.F('second_author__name')),
)
diff --git a/tests/ordering/tests.py b/tests/ordering/tests.py
index afc8791a05..5ee3c60324 100644
--- a/tests/ordering/tests.py
+++ b/tests/ordering/tests.py
@@ -485,7 +485,7 @@ class OrderingTests(TestCase):
def test_deprecated_values_annotate(self):
msg = (
"Article QuerySet won't use Meta.ordering in Django 3.1. Add "
- ".order_by('-pub_date', 'headline', OrderBy(F(author__name), "
+ ".order_by('-pub_date', F(headline), OrderBy(F(author__name), "
"descending=False), OrderBy(F(second_author__name), "
"descending=False)) to retain the current query."
)