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.
This commit is contained in:
kmwenja 2015-10-09 15:08:18 +03:00 committed by Kennedy Mwenja
parent af22135abb
commit bf498b293e
2 changed files with 9 additions and 9 deletions

View File

@ -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()

View File

@ -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)