Resolve default ordering warnings from tests

This commit is contained in:
AlanCoding
2019-05-02 18:00:18 -04:00
parent f174902bb2
commit f4c18843a3
13 changed files with 82 additions and 0 deletions

View File

@@ -402,6 +402,8 @@ class OrderByBackend(BaseFilterBackend):
order_by = value.split(',')
else:
order_by = (value,)
if order_by is None:
order_by = self.get_default_ordering(view)
if order_by:
order_by = self._validate_ordering_fields(queryset.model, order_by)
@@ -428,6 +430,12 @@ class OrderByBackend(BaseFilterBackend):
# Return a 400 for invalid field names.
raise ParseError(*e.args)
def get_default_ordering(self, view):
ordering = getattr(view, 'ordering', None)
if isinstance(ordering, str):
return (ordering,)
return ordering
def _validate_ordering_fields(self, model, order_by):
for field_name in order_by:
# strip off the negation prefix `-` if it exists