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

Conversation

housleyjk
Copy link
Contributor

@housleyjk housleyjk commented Dec 6, 2018

Fixes #521

Description of the Change

The details in #521 are not clear, but I ran into a very similar SkipField issue when trying to use HyperlinkedRelatedFields and the RelatedMixin. The HyperlinkedRelatedField generates the correct url for the related link (e.g. /authors/1/entries), but visiting the url gives a 500 error because of an unhandled SkipField exception. In other words, without these changes, it is possible to give a related link in the payload, but that link won't actually work.

Checklist

  • PR only contains one change (considered splitting up PR)
  • unit-test added (I couldn't get the unit tests to all work prior to making these changes, but I was able to trigger and fix this issue with the existing test_retrieve_related_many() test. So this code is covered by unit tests.)
  • NO documentation changes necessary, bug fix
  • changelog entry added to CHANGELOG.md
  • author name in AUTHORS

@sliverc sliverc self-requested a review December 7, 2018 13:03
Copy link
Member

@sliverc sliverc left a comment

Choose a reason for hiding this comment

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

Thanks @housleyjk This looks like a very reasonable fix. Testing should be improved though - see inline comments above.

@@ -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?

@codecov
Copy link

codecov bot commented Dec 7, 2018

Codecov Report

Merging #529 into master will increase coverage by 0.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #529      +/-   ##
==========================================
+ Coverage   94.31%   94.33%   +0.01%     
==========================================
  Files          60       60              
  Lines        3713     3725      +12     
==========================================
+ Hits         3502     3514      +12     
  Misses        211      211
Impacted Files Coverage Δ
example/models.py 90.9% <ø> (ø) ⬆️
example/settings/test.py 100% <100%> (ø) ⬆️
rest_framework_json_api/views.py 94.29% <100%> (+0.07%) ⬆️
example/tests/test_views.py 100% <100%> (ø) ⬆️
example/serializers.py 100% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update f309c7c...46bc6c2. Read the comment docs.

@housleyjk
Copy link
Contributor Author

Ok, I've improved the testing for this feature as requested and done my best to verify that it is correct. I hope you will forgive me renaming comment_set -> comments. I thought it was more consistent with the related name entries already in use.

@sliverc
Copy link
Member

sliverc commented Dec 10, 2018

Thanks a lot @housleyjk I totally agree renaming comments to comment_set is much cleaner.

@sliverc sliverc changed the title Allow HyperlinkedRelatedField to be used with retrieve_related (#521) Allow HyperlinkedRelatedField to be used with related urls Dec 10, 2018
@sliverc sliverc merged commit 8c2b5af into django-json-api:master Dec 10, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

HyperlinkedRelatedField is incompatible with HyperlinkedModelSerializer
2 participants