From bf498b293eb444b5dd94f6352f87107db5c61762 Mon Sep 17 00:00:00 2001 From: kmwenja Date: Fri, 9 Oct 2015 15:08:18 +0300 Subject: [PATCH] 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. --- rest_framework/filters.py | 14 +++++++------- tests/test_filters.py | 4 ++-- 2 files changed, 9 insertions(+), 9 deletions(-) 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)