-
Notifications
You must be signed in to change notification settings - Fork 300
Allow get_serializer_class to be overwritten for related urls without defining serializer_class fallback #904
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
Allow get_serializer_class to be overwritten for related urls without defining serializer_class fallback #904
Conversation
Codecov Report
@@ Coverage Diff @@
## master #904 +/- ##
=======================================
Coverage 97.67% 97.67%
=======================================
Files 58 58
Lines 3101 3101
=======================================
Hits 3029 3029
Misses 72 72
Continue to review full report at Codecov.
|
rest_framework_json_api/relations.py
Outdated
@@ -27,6 +27,7 @@ | |||
"related_link_view_name", | |||
"related_link_lookup_field", | |||
"related_link_url_kwarg", | |||
"related_link_related_name", |
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.
This fixes the fact that getting related view's serializer gets it from the super class, rather than the current class.
The PR description states that this PR is addressing issue that related view does not properly get parent serializer class (it seems to be another clean up after #860). I guess this change addresses this issue.
So at a quick glance for me it is not quite clear why introducing of a new links parameters is needed to fix this issue. Can you elaborate?
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.
Explanation 2:
We need to add this parameter to specify the the related
link for hyperlink fields (which can, most of the time, have a different related name).
For example in the example serializers, the field_name
is comments_hyperlinked
:
comments_hyperlinked = relations.HyperlinkedRelatedField(
related_link_view_name="entry-comments",
And the related view is:
url(
r"entries/(?P<entry_pk>[^/.]+)/comments$",
CommentViewSet.as_view({"get": "list"}),
name="entry-comments",
),
So comments_hyperlinked
relates to comments
.
But here, we get the field_names, which will create such a url: /entries/1/comments_hyperlinked
, which isn't really what we want.
@@ -392,6 +393,9 @@ def _find_related_view(self, view_endpoints, related_serializer, parent_view): | |||
|
|||
def _field_is_one_or_many(self, field, view): | |||
serializer = view.get_serializer() | |||
if field not in serializer.fields: |
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.
Thanks for your explanation. When reading through it seems that you are trying to address 3 different issues in one PR (each explanation 1).
To ensure that each single issue is properly tested and better way of reviewing could you split up this PR? Out of urgency point number 1 would be high priority as it is a regression (I will see whether I will get to that as well at some point later on if you won't get to it before me). |
I added a test (resp. updated as before the test was invalid) and added changelog entry for the parent super class call issue. Merging this. For the other two issue feel free to either create another PR or issue. Thanks for your testing effort - really appreciated that you found those hidden away bugs in the latest version. |
I checked the issue with openapi schema generation, and it seems to be with related views as well. I'll open to issues about those problems, and let experts handle them :) |
Description of the Change
This fixes the fact that getting related view's serializer gets it from the super class, rather than the current class.
Bug can be reproduced by using:
instead of:
Navigating to
entries/1/comments/
now will result in an error stating that noget_serializer_class
has been defined, even though it exists.Checklist
addedupdatedCHANGELOG.md
updated (only for user relevant changes)AUTHORS