mirror of
https://github.com/django/django.git
synced 2025-10-31 09:41:08 +00:00
Fixed #24479 -- Added system check to prevent both ordering and order_wrt.
This commit is contained in:
@@ -1479,7 +1479,17 @@ class Model(six.with_metaclass(ModelBase)):
|
||||
def _check_ordering(cls):
|
||||
""" Check "ordering" option -- is it a list of strings and do all fields
|
||||
exist? """
|
||||
if not cls._meta.ordering:
|
||||
if cls._meta._ordering_clash:
|
||||
return [
|
||||
checks.Error(
|
||||
"'ordering' and 'order_with_respect_to' cannot be used together.",
|
||||
hint=None,
|
||||
obj=cls,
|
||||
id='models.E021',
|
||||
),
|
||||
]
|
||||
|
||||
if cls._meta.order_with_respect_to or not cls._meta.ordering:
|
||||
return []
|
||||
|
||||
if not isinstance(cls._meta.ordering, (list, tuple)):
|
||||
@@ -1502,9 +1512,6 @@ class Model(six.with_metaclass(ModelBase)):
|
||||
# Convert "-field" to "field".
|
||||
fields = ((f[1:] if f.startswith('-') else f) for f in fields)
|
||||
|
||||
fields = (f for f in fields if
|
||||
f != '_order' or not cls._meta.order_with_respect_to)
|
||||
|
||||
# Skip ordering in the format field1__field2 (FIXME: checking
|
||||
# this format would be nice, but it's a little fiddly).
|
||||
fields = (f for f in fields if '__' not in f)
|
||||
|
||||
@@ -100,6 +100,7 @@ class Options(object):
|
||||
self.verbose_name_plural = None
|
||||
self.db_table = ''
|
||||
self.ordering = []
|
||||
self._ordering_clash = False
|
||||
self.unique_together = []
|
||||
self.index_together = []
|
||||
self.select_on_save = False
|
||||
@@ -234,6 +235,9 @@ class Options(object):
|
||||
if self.verbose_name_plural is None:
|
||||
self.verbose_name_plural = string_concat(self.verbose_name, 's')
|
||||
|
||||
# order_with_respect_and ordering are mutually exclusive.
|
||||
self._ordering_clash = bool(self.ordering and self.order_with_respect_to)
|
||||
|
||||
# Any leftover attributes must be invalid.
|
||||
if meta_attrs != {}:
|
||||
raise TypeError("'class Meta' got invalid attribute(s): %s" % ','.join(meta_attrs.keys()))
|
||||
|
||||
Reference in New Issue
Block a user