Skip to content

How to add links (self, related) to model and its relationships? #132

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

Closed
jklapuch opened this issue Oct 14, 2015 · 6 comments
Closed

How to add links (self, related) to model and its relationships? #132

jklapuch opened this issue Oct 14, 2015 · 6 comments

Comments

@jklapuch
Copy link

I figured only self links to main model by adding 'url' to fields in serializer.
How to add "related" link to it? And how to add all links to model relationships?

Thanks

@jsenecal
Copy link
Member

ResourceRelatedField and RelationshipView are your friends - for the latter, see https://github.com/django-json-api/django-rest-framework-json-api/blob/develop/example/urls_test.py#L25 for information on how to implement it.

For related links, you will obviously need to setup your own views before referencing them in your ResourceRelatedFields

@jklapuch
Copy link
Author

Thank you,
I used ResourceRelatedField before (without RelationshipView), but my mistake was setting fields

 "related_link_view_name", 
 "self_link_view_name"

The main problem was in my understanding of self and related links for relationships. I wanted for model "user" and relationship "list":

"self": "/lists/<list_id> 
"related": "/lists"

instead of /users/user_id/relationships/lists. But it's so right in jsonapi specification and my shape is valid only for included objects

@jsenecal
Copy link
Member

Glad I could help 👍 - If you'd like to contribute to the documentation with your findings it'd be awesome :)

@jsenecal
Copy link
Member

Lets keep that open to remind us to document this :)

@jsenecal jsenecal reopened this Oct 15, 2015
@jklapuch
Copy link
Author

ok, sorry.

@jerel
Copy link
Member

jerel commented Apr 11, 2016

Fixed by #194

@jerel jerel closed this as completed Apr 11, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants