-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
Use get_serializer_class in ordering filter #3487
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use get_serializer_class in ordering filter #3487
Conversation
|
||
view = OrderingListView.as_view() | ||
request = factory.get('/', {'ordering': 'text'}) | ||
# BUG: I think this should raise ImproperlyConfigured |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you know why it doesn't ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@xordoquy Sorry, I realized that the get_serializer_class
method actually has its own assert to check for serializer_class
: https://github.com/tomchristie/django-rest-framework/blob/master/rest_framework/generics.py#L123.
On that note, is it better to get the error from get_serializer_class
(AssertionError
) or from remove_invalid_fields
(ImproperlyConfigured
)? I can rewrite the patch to either catch the AssertionError
in get_serializer_class
or let it pass right up.
@xordoquy I preferred raising |
@xordoquy would it be presumptuous to update the docs at this point? |
We'll take some time to review it first. |
OrderingFilter backend checks whether a view specifies an ordering fields or serializer_class attribute. Views can however override get_serializer_class to determine the serializer_class but the OrderingFilter doesn't catch this. This patch fixes that.
just checking that the ImproperlyConfigured error is raised if ordering_fields, serializer_class and get_serializer_class are not provided in the view.
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.
c655de2
to
bf498b2
Compare
Current coverage is 91.43%@@ master #3487 diff @@
=======================================
Files 51 49 -2
Lines 5476 5356 -120
Methods 0 0
Branches 0 0
=======================================
- Hits 5005 4897 -108
+ Misses 471 459 -12
Partials 0 0
|
@xordoquy Updated the PR against master. Apologies for the lack of love. |
Perfectly reasonable, yup. Thanks! |
OrderingFilter
backend checks whether a view specifies anordering_fields
orserializer_class
attribute. Views can howeveroverride
get_serializer_class
to determine the serializer classbut the
OrderingFilter
doesn't catch this. This patch fixes that.Here's an example of a view that would benefit from this patch:
This would formerly raise an
ImproperlyConfigured
error.