-
Notifications
You must be signed in to change notification settings - Fork 299
Fix related resource on inherited polymorphic model #767
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
Fix related resource on inherited polymorphic model #767
Conversation
When adding a related resource to an inherited polymorphic model, that is absent on the base model, the related resource type is incorrectly resolved in get_related_resource_type. Introduce a LabResults model to the example code and add it as a related resource on the ResearchProjectSerializer to provoke an error. This already causes the test-suite to fail, so no new test has been added.
In get_related_resource_type, check if the parent_serializer is polymorphic. If so, resolve the parent_model using the parent serializer's instance to correctly handle related resources on polymorphic models.
Codecov Report
@@ Coverage Diff @@ ## master #767 +/- ## ======================================= Coverage 97.15% 97.16% ======================================= Files 54 55 +1 Lines 2816 2825 +9 ======================================= + Hits 2736 2745 +9 Misses 80 80
Continue to review full report at Codecov.
|
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 picking this up! This looks good to me.
Thanks for the quick merge, your work is much appreciated.
Fixes #621
Description of the Change
When adding a
ResourceRelatedField
to a serializer that is included in thepolymorphic_serializers
list of a polymorphic serializer, the
parent_model
is not correctly resolved inget_related_resource_type
: instead of resolving to the inherited type, the base type is resolved. This can cause anAttributeError
if the field in question is not present on the base model.We ran into this, and then found the closed and seemingly abandoned issue referenced above. This PR implements the fix suggested by the original author, who deserves all the credit. The example code has been expanded to trigger the error when running the test-suite.
Thanks in advance for your time and please let us know if you have any suggestions for improvement.
Checklist
(削除) unit-test added (削除ここまで)test-suite failure is introduced, but no unit-test added(削除) documentation updated (削除ここまで)unnecessaryCHANGELOG.md
updated (only for user relevant changes)AUTHORS