-
Notifications
You must be signed in to change notification settings - Fork 300
get_related_resource_type failing for polymorphic serializers #621
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
Comments
It would be great if you could open a PR which reproduces this error and a potential fix. Once a reproducing test is ready it might also be clearer whether your suggested fix works or whether there are better solutions. |
@favll Probably I ran into the same problem. Any news on this? |
@favll @ro70 did you make sure you're properly using the Defining a polymorphic serializer is enough when using it directly for its model endpoint, but for related models, as in your example @favll , a |
As there hasn't been any feedback and as of comment of @David-Guillot I assume this issue has been solved so closing. Please don't hesitate to comment if this issue still exists though. |
Fixes #621 When adding a ResourceRelatedField to a serializer that is included in the polymorphic_serializers list of a polymorphic serializer, the parent_model is not correctly resolved in get_related_resource_type: instead of resolving to the inherited type, the base type is resolved. This can cause an AttributeError 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.
We have run into a problem where DRF JSON API resolves the wrong related resource type when trying to include a resource using a polymorphic serializer. Specifically, DRF JSON API will resolve the base model instead of the inherited model.
Below is an abbreviated example for the issue we are running into. In the following example
utils.get_related_resource_type
will resolve theparent_model
tomodels.Product
instead ofmodels.ArtProduct
(line here). Subsequently, the method will try to accessart_details
on theparent_model
which will fail since it only exists onmodels.ArtProduct
and notmodels.Product
(line here).A proposed fix would be to resolve the
parent_model
by checking if theparent_serializer
is a polymorphic serializer and resolving theparent_model
with something along the lines of:parent_serializer.get_polymorphic_serializer_for_instance(parent_serializer.instance).Meta.model
If desired, I can provide further clarification and prepare a PR.
The text was updated successfully, but these errors were encountered: