From ec2c9c353113bb1db6e32ed3f0b6c28bc06ca2eb Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Thu, 14 Jun 2018 14:47:20 -0400 Subject: Refs #29428 -- Fixed admin check crash when using a query expression in ModelAdmin.ordering. --- django/contrib/admin/checks.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'django') diff --git a/django/contrib/admin/checks.py b/django/contrib/admin/checks.py index e0f3a21550..cf5b312369 100644 --- a/django/contrib/admin/checks.py +++ b/django/contrib/admin/checks.py @@ -10,6 +10,7 @@ from django.core import checks from django.core.exceptions import FieldDoesNotExist from django.db import models from django.db.models.constants import LOOKUP_SEP +from django.db.models.expressions import Combinable, F, OrderBy from django.forms.models import ( BaseModelForm, BaseModelFormSet, _get_foreign_key, ) @@ -489,7 +490,13 @@ class BaseModelAdminChecks: def _check_ordering_item(self, obj, model, field_name, label): """ Check that `ordering` refers to existing fields. """ - + if isinstance(field_name, (Combinable, OrderBy)): + if not isinstance(field_name, OrderBy): + field_name = field_name.asc() + if isinstance(field_name.expression, F): + field_name = field_name.expression.name + else: + return [] if field_name == '?' and len(obj.ordering) != 1: return [ checks.Error( -- cgit v1.3