-
Notifications
You must be signed in to change notification settings - Fork 298
Add HyperlinkedRelatedField and SerializerMethodHyperlinkedRelatedField fields #445
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
Conversation
Thanks for looking into this. This approach is more DRF like so I prefer it over #443. I hoped though that HyperlinkRelatedField
doesn't execute the query itself. But when I looked at the DRF code it seems it still needs SkipField
workaround.
I have a feeling that this is actually an issue in DRF itself as HyperlinkRelatedField
does execute a query for m2m relations even though it doesn't need it. If it is fixed there we could adjust this PR accordingly.
So what I suggest before we move forward is to investigate whether the same problem with executing of queries also exist in DRF HyperlinkRelatedField
itself. If yes we open an issue in DRF and see what the feedback is. Once we have feedback I think we better now how to adjust this PR accordingly.
Would be great if you would have time to follow up on this investigation but also understand when you can't. I am also interested in a fix for this but I am not sure when I will have time to get to it.
@sliverc I think DRF's HyperLinkRelatedField
works correct with many=True
Let's see the docs, tracks
field:
{
'album_name': 'Graceland',
'artist': 'Paul Simon',
'tracks': [
'http://www.example.com/api/tracks/45/',
'http://www.example.com/api/tracks/46/',
'http://www.example.com/api/tracks/47/',
...
]
}
It generates one link per each item in the list, but not one link for related collection, like we do in json-api.
So, I think we have to implement custom logic for this with ManyRelatedFieldWithNoData
.
Are there other things you're confused with ?
@Anton-Shutik
This makes total sense. Thanks for the follow up. Let's do it the way you have proposed it then it this PR. Could you follow up with tests, docs CI fixes in this PR?
Once done I would do a more detailed review of the code.
Just on another note why I prefer this to render_data
flag.
I can simply overwrite serializer_related_field
like:
class MySerializer(ModelSerializer): serializer_related_field = HyperlinkRelatedField
this way all related fields are only represented as links without defining each one again in the serializer.
3cf26c8
to
4eb75b3
Compare
Codecov Report
@@ Coverage Diff @@ ## master #445 +/- ## ========================================= + Coverage 92.68% 93.1% +0.41% ========================================= Files 54 54 Lines 2858 3000 +142 ========================================= + Hits 2649 2793 +144 + Misses 209 207 -2
Continue to review full report at Codecov.
|
@sliverc added test, updated the docs. Could you pls do a review ?
96fadd3
to
4e2000e
Compare
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.
A few small changes needed and this should be ready for merging (see inline comments).
Also changelog and authors files should be updated as well.
docs/usage.md
Outdated
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.
Maybe we should also mention lower payload here?
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.
Added
rest_framework_json_api/relations.py
Outdated
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.
In DRF it is called HyperlinkedRelatedField
with a lower case linked
. For consistency we should also stick to the same naming. This counts for all occurenced of HyperLinked changing to Hyperlinked.
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.
Done
example/tests/test_relations.py
Outdated
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.
shouldn't this be called get_comments
? It would be good to expand the test so get_blog
and get_comments
is covered as well.
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 test code is actually not covered by a test. Can you expand your test accordingly?
example/views.py
Outdated
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.
Simply returning first entry seems strange to me. It would also be good to add a test which actually covers this though.
@sliverc I did requested changes, would you mind taking another look ?
And when it is going to released ?
157d878
to
8210c5e
Compare
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.
One small change and we are ready for merging.
docs/usage.md
Outdated
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.
Documentation needs to be updated to use the lowercase linked
version as well.
8210c5e
to
f433b70
Compare
@sliverc updated. What about release date ?
Thanks
f433b70
to
3e5f0a8
Compare
Merged. Thanks for your effort. In terms of release date: we do not have a fix release schedule but try to release often but not after every PR.
jasonm
commented
Jul 28, 2018
What good timing - we were looking for exactly this today! (Use case of avoiding N+1 API queries from DS.JSONAPIAdapter
) Thanks for the contribution @Anton-Shutik and merging @sliverc !
@jasonm Enjoy! :)
benjaminlong
commented
Feb 27, 2019
Thanks @Anton-Shutik ! Just integrated your work, it was smooth. Love it !
Uh oh!
There was an error while loading. Please reload this page.
Fixes #296
Description of the Change
Include only links in relationships objects by introducing
HyperlinkedRelatedField
andSerializerMethodHyperlinkedRelatedField
.Checklist
CHANGELOG.md
AUTHORS