Django REST Framework: when to create a hyperlinked resource and when nested resource? How to POST a nested resource? -
i'm building rest web api using django rest framework. things going great, have, stumbled upon problem nested resources. @ first, relationships in rest api hyperlinked. post, example, looked this:
{ "path": "http://api.myproject.com/posts/1.json", "id": 1, "author": "http://api.myproject.com/users/broak.json", "image": "/images/posts/cxyuzlpo.jpg", "header": "who i?", "footer": "i champion!", "date": "2014-11-09 15:16", "likes": "http://api.myproject.com/posts/1/likes.json", "comments": "http://api.myproject.com/posts/1/comments.json", "likes_count": 0, "comments_count": 0 }
the relationship between post , author (user) hyperlinked. when want create new post, need specify right hyperlink specific user - works fine.
when calling list of posts, things become inefficient, because have create api phone call every author every post. solved using nested resources instead of hyperlinked resources, every post contains info author.
{ "path": "http://api.myproject.com/posts/1.json", "id": 1, "author": { "email": "broak@gmail.com" "username": "broak", "first_name: "john", "last_name": "broak", "is_staff": false, "is_active": true, "last_login": "02-26-2016" }, "image": "/images/posts/cxyuzlpo.jpg", "header": "who i?", "footer": "i champion!", "date": "2014-11-09 15:16", "likes": "http://api.myproject.com/posts/1/likes.json", "comments": "http://api.myproject.com/posts/1/comments.json", "likes_count": 0, "comments_count": 0 }
my first question is: have guideline, whether should create nested info construction or separate endpoint hyperlink it.
my sec question is: when utilize author nested resource , want create new post, don't want specify info author (username, e-mail, ...). there way utilize link user create/update operation? or modify user id plenty fill in field?
if understood question correctly, want have expanded author while retrieving info , want send id or url in case of update , create.
1#
not guideline , totally depends on requirement of how api
going used.
2#
need extend userserializer
, override to_internal_value
. sample code might like
class mycustomserializer(userserializer): def to_internal_value(self, data): # info must valid user-detail url homecoming serializers.hyperlinkedrelatedfield(queryset=user.objects.all(), view_name='user-detail').to_internal_value(data)
notice must have endpoint user-detail in able work hyperlinkedrelatedfield.
so if want able send id
sample code might like
class mycustomserializer(userserializer): # info must valid user id def to_internal_value(self, data): homecoming serializers.primarykeyrelatedfield(queryset=user.objects.all()).to_internal_value(data)
however maintain consistency in sending foreignkey field in post/put/patch
. (always either url or id).
then utilize in code like
class postserializer(serializers.hyperlinkedmodelserializer): author = mycustomserializer() class meta: model = post
please see documentation on writable nested serializers post
on nested resource.
django rest hyperlink resources django-rest-framework
No comments:
Post a Comment