Skip to content

Commit c655de2

Browse files
committed
Raise ImproperlyConfigured when missing serializer_class
get_serializer_class raises an AssertionError if no serializer_class is found (either as a class attribute or from an overriding method). This commit catches it and raises ImproperlyConfigured instead.
1 parent c6b5ae0 commit c655de2

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

rest_framework/filters.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -158,14 +158,14 @@ def remove_invalid_fields(self, queryset, fields, view):
158158

159159
if valid_fields is None:
160160
# Default to allowing filtering on serializer fields
161-
serializer_class = getattr(view, 'serializer_class')
162-
if serializer_class is None:
161+
try:
163162
serializer_class = view.get_serializer_class()
164-
if serializer_class is None:
165-
msg = ("Cannot use %s on a view which does not have either a "
166-
"'serializer_class', an overriding 'get_serializer_class' "
167-
"or 'ordering_fields' attribute.")
168-
raise ImproperlyConfigured(msg % self.__class__.__name__)
163+
except AssertionError: # raised if no serializer_class was found
164+
msg = ("Cannot use %s on a view which does not have either a "
165+
"'serializer_class', an overriding 'get_serializer_class' "
166+
"or 'ordering_fields' attribute.")
167+
raise ImproperlyConfigured(msg % self.__class__.__name__)
168+
169169
valid_fields = [
170170
field.source or field_name
171171
for field_name, field in serializer_class().fields.items()

tests/test_filters.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from decimal import Decimal
66

77
from django.conf.urls import url
8+
from django.core.exceptions import ImproperlyConfigured
89
from django.core.urlresolvers import reverse
910
from django.db import models
1011
from django.test import TestCase
@@ -773,8 +774,7 @@ class OrderingListView(generics.ListAPIView):
773774

774775
view = OrderingListView.as_view()
775776
request = factory.get('/', {'ordering': 'text'})
776-
# BUG: I think this should raise ImproperlyConfigured
777-
with self.assertRaises(AssertionError):
777+
with self.assertRaises(ImproperlyConfigured):
778778
view(request)
779779

780780

0 commit comments

Comments
 (0)