-
Notifications
You must be signed in to change notification settings - Fork 28
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
Update get_serializer_class
to consider the request method
#77
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
e040c6f
WIP - Fix GenericAPIView::get_serializer_class
pamella 9308d8d
Merge branch 'feat/setup-ruff' into fix/get_serializer_class
pamella 65a7602
Merge branch 'feat/setup-ruff' into fix/get_serializer_class
pamella 2564d9a
Merge branch 'main' into fix/get_serializer_class
pamella f2e79d2
Rename for clarity
pamella f2e10c6
Update .gitignore
pamella 0affe77
Add more tests
pamella 9cd1e2d
Rename for clarity
pamella 1d0ad37
Refactor GenericAPIViewGetSerializerClassTests
pamella 58e95c3
Update code to explict handle HTTP request methods
pamella File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,11 +2,16 @@ | |
|
||
from rest_framework import generics, mixins | ||
|
||
from .mixins import CreateModelMixin, ListModelMixin, RetrieveModelMixin, UpdateModelMixin | ||
from .mixins import ( | ||
CreateModelMixin, | ||
ListModelMixin, | ||
RetrieveModelMixin, | ||
UpdateModelMixin, | ||
) | ||
|
||
|
||
class GenericAPIView(generics.GenericAPIView): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
def get_serializer_class(self): | ||
def _get_serializer_class(self): | ||
""" | ||
Return the class to use for the serializer. | ||
Defaults to using `self.serializer_class`. | ||
|
@@ -25,6 +30,42 @@ def get_serializer_class(self): | |
|
||
return self.serializer_class | ||
|
||
def get_serializer_class(self): | ||
""" | ||
Return the class to use for the serializer. | ||
Defaults to using `self.serializer_class`. | ||
If the request method is GET, it tries to use `self.read_serializer_class`. | ||
If the request method is not GET, it tries to use `self.write_serializer_class`. | ||
If the specific serializer class for the request method is not set, it falls back to | ||
`self.serializer_class`. | ||
You may want to override this if you need to provide different | ||
serializations depending on the incoming request. | ||
(Eg. admins get full serialization, others get basic serialization) | ||
""" | ||
if hasattr(self, "request"): | ||
if self.request.method in ["GET", "HEAD", "OPTIONS", "TRACE"]: | ||
assert ( | ||
getattr(self, "read_serializer_class", None) is not None | ||
or self.serializer_class is not None | ||
), ( | ||
"'%s' should either include a `read_serializer_class` or `serializer_class` " | ||
"attribute, or override the `get_read_serializer_class()` or " | ||
"`get_serializer_class()` method." % self.__class__.__name__ | ||
) | ||
return self.get_read_serializer_class() | ||
elif self.request.method in ["POST", "PUT", "PATCH", "DELETE"]: | ||
assert ( | ||
getattr(self, "write_serializer_class", None) is not None | ||
or self.serializer_class is not None | ||
), ( | ||
"'%s' should either include a `write_serializer_class` or `serializer_class` " | ||
"attribute, or override the `get_write_serializer_class()` or " | ||
"`get_serializer_class()` method." % self.__class__.__name__ | ||
) | ||
return self.get_write_serializer_class() | ||
|
||
return self._get_serializer_class() | ||
pamella marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def get_read_serializer(self, *args, **kwargs): | ||
""" | ||
Return the serializer instance that should be used for serializing output. | ||
|
@@ -42,7 +83,7 @@ def get_read_serializer_class(self): | |
(Eg. admins get full serialization, others get basic serialization) | ||
""" | ||
if getattr(self, "read_serializer_class", None) is None: | ||
return self.get_serializer_class() | ||
return self._get_serializer_class() | ||
|
||
return self.read_serializer_class | ||
|
||
|
@@ -64,7 +105,7 @@ def get_write_serializer_class(self): | |
(Eg. admins can send extra fields, others cannot) | ||
""" | ||
if getattr(self, "write_serializer_class", None) is None: | ||
return self.get_serializer_class() | ||
return self._get_serializer_class() | ||
|
||
return self.write_serializer_class | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
GenericAPIViewGetSerializerClassTests
tests the changes made in this file.