Skip to content

Allow HyperlinkedRelatedField to be used with related urls #529

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

Merged
merged 5 commits into from
Dec 10, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ santiavenda <[email protected]>
Tim Selman <[email protected]>
Yaniv Peer <[email protected]>
Mohammed Ali Zubair <[email protected]>
Jason Housley <[email protected]>
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ any parts of the framework not mentioned in the documentation should generally b
* Avoid error with related urls when retrieving relationship which is referenced as `ForeignKey` on parent
* Do not render `write_only` relations
* Do not skip empty one-to-one relationships
* Handle SkipField exception in RelateMixin


## [2.6.0] - 2018-09-20
Expand Down
2 changes: 1 addition & 1 deletion example/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ class AuthorSerializer(serializers.ModelSerializer):
self_link_view_name='author-relationships',
queryset=AuthorBio.objects,
)
entries = relations.ResourceRelatedField(
entries = relations.HyperlinkedRelatedField(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not so ideal to change from ResourceRelatedField to HyperlinkedRelatedField as now the one but not the other will be tested.

Hence I think it would be better to add a new HyperlinkedRelatedField. Maybe comments could be used?

related_link_view_name='author-related',
self_link_view_name='author-relationships',
queryset=Entry.objects,
Expand Down
12 changes: 8 additions & 4 deletions rest_framework_json_api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from rest_framework.relations import PKOnlyObject
from rest_framework.response import Response
from rest_framework.reverse import reverse
from rest_framework.serializers import Serializer
from rest_framework.serializers import Serializer, SkipField

from rest_framework_json_api.exceptions import Conflict
from rest_framework_json_api.serializers import ResourceIdentifierObjectSerializer
Expand Down Expand Up @@ -166,10 +166,14 @@ def get_related_instance(self):
field = parent_serializer.fields.get(field_name, None)

if field is not None:
instance = field.get_attribute(parent_obj)
if isinstance(instance, PKOnlyObject):
# need whole object
try:
instance = field.get_attribute(parent_obj)
except SkipField:
instance = get_attribute(parent_obj, field.source_attrs)
else:
if isinstance(instance, PKOnlyObject):
# need whole object
instance = get_attribute(parent_obj, field.source_attrs)
return instance
else:
try:
Expand Down