Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use get_serializer_class for ordering fields #181

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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