diff --git a/rest_framework/filters.py b/rest_framework/filters.py index dfb4ec2c2..a510d3bf6 100644 --- a/rest_framework/filters.py +++ b/rest_framework/filters.py @@ -227,14 +227,14 @@ class OrderingFilter(BaseFilterBackend): if valid_fields is None: # Default to allowing filtering on serializer fields - serializer_class = getattr(view, 'serializer_class') - if serializer_class is None: + try: serializer_class = view.get_serializer_class() - 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__) + 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__) + valid_fields = [ (field.source or field_name, field.label) for field_name, field in serializer_class().fields.items() diff --git a/tests/test_filters.py b/tests/test_filters.py index 9d9acdb7f..8493c96af 100644 --- a/tests/test_filters.py +++ b/tests/test_filters.py @@ -5,6 +5,7 @@ import unittest from decimal import Decimal from django.conf.urls import url +from django.core.exceptions import ImproperlyConfigured from django.core.urlresolvers import reverse from django.db import models from django.test import TestCase @@ -786,8 +787,7 @@ class OrderingFilterTests(TestCase): view = OrderingListView.as_view() request = factory.get('/', {'ordering': 'text'}) - # BUG: I think this should raise ImproperlyConfigured - with self.assertRaises(AssertionError): + with self.assertRaises(ImproperlyConfigured): view(request)