- Notifications
You must be signed in to change notification settings - Fork302
Add support for related links using parent view and its permissions#451
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
ffe61c6
196d8ba
fc52dc4
73d51a5
7677042
bde21ec
9e78e67
d9b5f08
9decc1f
77ac0b2
6e0b47c
a210e63
c148507
c0f0dab
228b1e8
8191d6d
b8a902f
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -443,6 +443,53 @@ class LineItemViewSet(viewsets.ModelViewSet): | ||
not render `data`. Use this in case you only need links of relationships and want to lower payload | ||
and increase performance. | ||
#### Related urls | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Do you think we should combine this documentation withhttps://django-rest-framework-json-api.readthedocs.io/en/stable/usage.html#related-fields ? Maybe we do not need to document overwriting of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. It is still working and can be used. I think we can remove the docs when we deprecate this stuff. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. I have just read through the documentation again and I am not sure whether it is clear why there are two different ways to basically do the same. I think the one difference is that with the old way overwriting Somehow we should try to merge those two documentation pieces into one, recommending to use I first was planning to work on this but I do not really have time at hand. So if you have a suggestion feel free. Otherwise I might get to it at a later point. | ||
There is a nice way to handle "related" urls like `/orders/3/lineitems/` or `/orders/3/customer/`. | ||
All you need is just add to `urls.py`: | ||
```python | ||
url(r'^orders/(?P<pk>[^/.]+)/$', | ||
OrderViewSet.as_view({'get': 'retrieve'}), | ||
name='order-detail'), | ||
url(r'^orders/(?P<pk>[^/.]+)/(?P<related_field>\w+)/$', | ||
OrderViewSet.as_view({'get': 'retrieve_related'}), | ||
name='order-related'), | ||
``` | ||
Make sure that RelatedField declaration has `related_link_url_kwarg='pk'` or simply skipped (will be set by default): | ||
```python | ||
line_items = ResourceRelatedField( | ||
queryset=LineItem.objects, | ||
many=True, | ||
related_link_view_name='order-related', | ||
related_link_url_kwarg='pk', | ||
self_link_view_name='order-relationships' | ||
) | ||
customer = ResourceRelatedField( | ||
queryset=Customer.objects, | ||
related_link_view_name='order-related', | ||
self_link_view_name='order-relationships' | ||
) | ||
``` | ||
And, the most important part - declare serializer for each related entity: | ||
```python | ||
class OrderSerializer(serializers.HyperlinkedModelSerializer): | ||
... | ||
related_serializers = { | ||
'customer': 'example.serializers.CustomerSerializer', | ||
'line_items': 'example.serializers.LineItemSerializer' | ||
} | ||
``` | ||
Or, if you already have `included_serializers` declared and your `related_serializers` look the same, just skip it: | ||
```python | ||
class OrderSerializer(serializers.HyperlinkedModelSerializer): | ||
... | ||
included_serializers = { | ||
'customer': 'example.serializers.CustomerSerializer', | ||
'line_items': 'example.serializers.LineItemSerializer' | ||
} | ||
``` | ||
### RelationshipView | ||
`rest_framework_json_api.views.RelationshipView` is used to build | ||
relationship views (see the | ||