-
Notifications
You must be signed in to change notification settings - Fork 301
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 I have a feeling that this is actually an issue in DRF itself as So what I suggest before we move forward is to investigate whether the same problem with executing of queries also exist in DRF 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
It generates one link per each item in the list, but not one link for related collection, like we do in json-api. Are there other things you're confused with ? |
@Anton-Shutik |
Just on another note why I prefer this to I can simply overwrite 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
@@ -427,6 +429,12 @@ class LineItemViewSet(viewsets.ModelViewSet): | |||
return queryset | |||
``` | |||
|
|||
#### HyperLinkedRelatedField | |||
|
|||
In order to improve performance by saving some sql queries we can skip `data` |
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
@@ -140,6 +125,78 @@ def get_links(self, obj=None, lookup_field='pk'): | |||
return_data.update({'related': related_link}) | |||
return return_data | |||
|
|||
|
|||
class HyperLinkedRelatedField(HyperLinkedMixin, SkipDataMixin, RelatedField): |
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
def get_blog(self, obj): | ||
return obj.blog | ||
|
||
def get_authors(self, obj): |
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.
def get_object(self): | ||
entry_pk = self.kwargs.get('entry_pk', None) | ||
if entry_pk is not None: | ||
return Entry.objects.get(id=entry_pk).blog |
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
# Handle featured | ||
entry_pk = self.kwargs.get('entry_pk', None) | ||
if entry_pk is not None: | ||
return Entry.objects.first() |
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 ? |
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
@@ -435,6 +437,12 @@ class LineItemViewSet(viewsets.ModelViewSet): | |||
return queryset | |||
``` | |||
|
|||
#### HyperLinkedRelatedField |
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 ? |
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. |
What good timing - we were looking for exactly this today! (Use case of avoiding N+1 API queries from |
@jasonm Enjoy! :) |
Thanks @Anton-Shutik ! Just integrated your work, it was smooth. Love it ! |
Fixes #296
Description of the Change
Include only links in relationships objects by introducing
HyperlinkedRelatedField
andSerializerMethodHyperlinkedRelatedField
.Checklist
CHANGELOG.md
AUTHORS