Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc655de2

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

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

‎rest_framework/filters.py‎

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

159159
ifvalid_fieldsisNone:
160160
# Default to allowing filtering on serializer fields
161-
serializer_class=getattr(view,'serializer_class')
162-
ifserializer_classisNone:
161+
try:
163162
serializer_class=view.get_serializer_class()
164-
ifserializer_classisNone:
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-
raiseImproperlyConfigured(msg%self.__class__.__name__)
163+
exceptAssertionError:# 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+
raiseImproperlyConfigured(msg%self.__class__.__name__)
168+
169169
valid_fields= [
170170
field.sourceorfield_name
171171
forfield_name,fieldinserializer_class().fields.items()

‎tests/test_filters.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
fromdecimalimportDecimal
66

77
fromdjango.conf.urlsimporturl
8+
fromdjango.core.exceptionsimportImproperlyConfigured
89
fromdjango.core.urlresolversimportreverse
910
fromdjango.dbimportmodels
1011
fromdjango.testimportTestCase
@@ -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-
withself.assertRaises(AssertionError):
777+
withself.assertRaises(ImproperlyConfigured):
778778
view(request)
779779

780780

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp