How to use hyperlinks to represent relationships instead of primary keys in Django REST framework -
i want object index "resource_uri" instead id take usual way create model , views , serializers :
class user(basemodel): user = models.foreignkey(settings.auth_user_model) models.charfield() class userserailizers(serializers.modelserializer): class meta: model = user fields = ('id','user','formatted_address') classclass userlist(generics.listcreateapiview): queryset = image.objects.all() serializer_class = userserializer
when phone call < my_domain/user/ > response
{ id:1, name:'toto' }
but want have reply form:
{ 'url': my_domain/user/1/ 'name': 'toto' }
any thoughts?
if want hyperlink instead of primary key in model representations, have utilize either hyperlinkedmodelserializer
or more generic serializer
along hyperlinkedidentityfield
and/or hyperlinkedrelatedfield
. former looking for.
the hyperlinkedmodelserializer
class similar modelserializer
class except uses hyperlinks represent relationships, rather primary keys.
see django rest framework documentation more details.
django-models django-rest-framework jsonserializer
No comments:
Post a Comment