Allow .order_by() to pass expressions unchanged

Fixes: #257
fix_request_path_info
Diederik van der Boor 2017-05-22 12:52:17 +02:00
parent d4fde82600
commit bcb8b0d3a4
2 changed files with 11 additions and 3 deletions

View File

@ -172,10 +172,14 @@ class PolymorphicQuerySet(QuerySet):
additional_args = translate_polymorphic_filter_definitions_in_kwargs(self.model, kwargs, using=self.db) # filter_field='data' additional_args = translate_polymorphic_filter_definitions_in_kwargs(self.model, kwargs, using=self.db) # filter_field='data'
return super(PolymorphicQuerySet, self)._filter_or_exclude(negate, *(list(q_objects) + additional_args), **kwargs) return super(PolymorphicQuerySet, self)._filter_or_exclude(negate, *(list(q_objects) + additional_args), **kwargs)
def order_by(self, *args, **kwargs): def order_by(self, *field_names):
"""translate the field paths in the args, then call vanilla order_by.""" """translate the field paths in the args, then call vanilla order_by."""
new_args = [translate_polymorphic_field_path(self.model, a) for a in args] field_names = [
return super(PolymorphicQuerySet, self).order_by(*new_args, **kwargs) translate_polymorphic_field_path(self.model, a)
if isinstance(a, six.string_types) else a # allow expressions to pass unchanged
for a in field_names
]
return super(PolymorphicQuerySet, self).order_by(*field_names)
def defer(self, *fields): def defer(self, *fields):
""" """

View File

@ -11,6 +11,7 @@ from django.db import models
from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.models import ContentType
from django.db.models import Q, FieldDoesNotExist from django.db.models import Q, FieldDoesNotExist
from django.db.utils import DEFAULT_DB_ALIAS from django.db.utils import DEFAULT_DB_ALIAS
from django.utils import six
from django.db.models.fields.related import RelatedField from django.db.models.fields.related import RelatedField
if django.VERSION < (1, 6): if django.VERSION < (1, 6):
@ -145,6 +146,9 @@ def translate_polymorphic_field_path(queryset_model, field_path):
into modela__modelb__modelc__field3. into modela__modelb__modelc__field3.
Returns: translated path (unchanged, if no translation needed) Returns: translated path (unchanged, if no translation needed)
""" """
if not isinstance(field_path, six.string_types):
raise ValueError("Expected field name as string: {0}".format(field_path))
classname, sep, pure_field_path = field_path.partition('___') classname, sep, pure_field_path = field_path.partition('___')
if not sep: if not sep:
return field_path return field_path