Skip to content

Commit

Permalink
Merge pull request #181 from masterfloda/feature/use_get_serializer_c…
Browse files Browse the repository at this point in the history
…lass_for_ordering

use get_serializer_class for ordering fields
  • Loading branch information
aleontiev authored Aug 9, 2017
2 parents 9769149 + 9bf02ef commit a7bf662
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions dynamic_rest/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -651,15 +651,28 @@ def get_valid_fields(self, queryset, view, context={}):
"""
valid_fields = getattr(view, 'ordering_fields', self.ordering_fields)

# prefer the overriding method
if hasattr(view, 'get_serializer_class'):
try:
serializer_class = view.get_serializer_class()
except AssertionError:
# Raised by the default implementation if
# no serializer_class was found
serializer_class = None
# use the attribute
else:
serializer_class = getattr(view, 'serializer_class', None)

# neither a method nor an attribute has been specified
if serializer_class is None:
msg = (
"Cannot use %s on a view which does not have either a "
"'serializer_class' or an overriding 'get_serializer_class'."
)
raise ImproperlyConfigured(msg % self.__class__.__name__)

if valid_fields is None or valid_fields == '__all__':
# Default to allowing filtering on serializer fields
serializer_class = getattr(view, 'serializer_class')
if serializer_class is None:
msg = (
"Cannot use %s on a view which does not have either a "
"'serializer_class' or 'ordering_fields' attribute."
)
raise ImproperlyConfigured(msg % self.__class__.__name__)
valid_fields = [
(field_name, field.source or field_name)
for field_name, field in serializer_class().fields.items()
Expand All @@ -668,7 +681,6 @@ def get_valid_fields(self, queryset, view, context={}):
) and not field.source == '*'
]
else:
serializer_class = getattr(view, 'serializer_class')
valid_fields = [
(field_name, field.source or field_name)
for field_name, field in serializer_class().fields.items()
Expand Down

0 comments on commit a7bf662

Please sign in to comment.