diff options
| author | Jon Dufresne <jon.dufresne@gmail.com> | 2015-03-17 07:42:53 -0700 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2015-03-21 08:16:28 -0400 |
| commit | 966a29c2b83fed2fc7c2b5b09de80cbebc94ac74 (patch) | |
| tree | 05c6511b0c821b3be9731264acba1d32b31967e3 /django | |
| parent | e304e1344857facd6356af9def3a7c9a78c528f9 (diff) | |
Fixed #24479 -- Added system check to prevent both ordering and order_wrt.
Diffstat (limited to 'django')
| -rw-r--r-- | django/db/models/base.py | 15 | ||||
| -rw-r--r-- | django/db/models/options.py | 4 |
2 files changed, 15 insertions, 4 deletions
diff --git a/django/db/models/base.py b/django/db/models/base.py index 07a4f5f7e1..0a9a837692 100644 --- a/django/db/models/base.py +++ b/django/db/models/base.py @@ -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) diff --git a/django/db/models/options.py b/django/db/models/options.py index 0762aa8df3..4a2ab1036f 100644 --- a/django/db/models/options.py +++ b/django/db/models/options.py @@ -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())) |
