Repository: encode/django-rest-framework

Issue:
`OrderingFilter` should call `get_serializer_class()` to determine default fields.
## Checklist
- [X] I have verified that that issue exists against the `master` branch of Django REST framework.
- [X] I have searched for similar issues in both open and closed tickets and cannot find a duplicate.
- [X] This is not a usage question. (Those should be directed to the [discussion group](https://groups.google.com/forum/#!forum/django-rest-framework) instead.)
- [X] This cannot be dealt with as a third party library. (We prefer new functionality to be [in the form of third party libraries](http://www.django-rest-framework.org/topics/third-party-resources/#about-third-party-packages) where possible.)
- [X] I have reduced the issue to the simplest possible case.
- [ ] I have included a failing test as a pull request. (If you are unable to do so we can still accept the issue.)

GenericAPIView says 

>  You'll need to either set these attributes,
>  or override `get_queryset()`/`get_serializer_class()`.

But in the method **get_valid_fields** of the **OrderingFilter** class, it gets the serializer_class like so

``` python
serializer_class = getattr(view, 'serializer_class')
```

I believe it should be 

``` python
serializer_class = view.get_serializer_class()
```

Benjamin



Discussion:
Thanks for the well written issue :+1: 

Oh yeah, this is an actionable ticket! :clap: @bmampaey want to put a a PR together to address this?


Resolving patch:
diff --git a/rest_framework/filters.py b/rest_framework/filters.py
--- a/rest_framework/filters.py
+++ b/rest_framework/filters.py
@@ -222,24 +222,40 @@ def get_default_ordering(self, view):
             return (ordering,)
         return ordering
 
+    def get_default_valid_fields(self, queryset, view):
+        # If `ordering_fields` is not specified, then we determine a default
+        # based on the serializer class, if one exists on the view.
+        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
+        else:
+            serializer_class = getattr(view, 'serializer_class', None)
+
+        if serializer_class is None:
+            msg = (
+                "Cannot use %s on a view which does not have either a "
+                "'serializer_class', an overriding 'get_serializer_class' "
+                "or 'ordering_fields' attribute."
+            )
+            raise ImproperlyConfigured(msg % self.__class__.__name__)
+
+        return [
+            (field.source or field_name, field.label)
+            for field_name, field in serializer_class().fields.items()
+            if not getattr(field, 'write_only', False) and not field.source == '*'
+        ]
+
     def get_valid_fields(self, queryset, view):
         valid_fields = getattr(view, 'ordering_fields', self.ordering_fields)
 
         if valid_fields is None:
             # Default to allowing filtering on serializer fields
-            try:
-                serializer_class = view.get_serializer_class()
-            except AssertionError:  # raised if no serializer_class was found
-                msg = ("Cannot use %s on a view which does not have either a "
-                       "'serializer_class', an overriding 'get_serializer_class' "
-                       "or 'ordering_fields' attribute.")
-                raise ImproperlyConfigured(msg % self.__class__.__name__)
+            return self.get_default_valid_fields(queryset, view)
 
-            valid_fields = [
-                (field.source or field_name, field.label)
-                for field_name, field in serializer_class().fields.items()
-                if not getattr(field, 'write_only', False) and not field.source == '*'
-            ]
         elif valid_fields == '__all__':
             # View explicitly allows filtering on any model field
             valid_fields = [


Test patch: