HyperlinkedIdentityField
-1 publish = serializers.HyperlinkedIdentityField(view_name='ttt',lookup_field='publish_id',lookup_url_kwarg='pky')
创新互联坚持“要么做到,要么别承诺”的工作理念,服务领域包括:网站建设、成都网站建设、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的依安网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!
# class BookSerializer(serializers.Serializer):
# name = serializers.CharField()
# # publish = serializers.CharField()
# publish = serializers.HyperlinkedIdentityField(view_name='ttt',lookup_field='publish_id',lookup_url_kwarg='pky')
from rest_framework.exceptions import ValidationError
-2 view_name:路由的别名,lookup_field:根据表的哪个字段,来拼路径,lookup_url_kwarg:反向解析有名分组的名字
-3 写路由:url(r'^publish/(?P
-4 实例化序列化类的时候,需要把request对象传过去
book_ser=BookSerializer(ret,many=True,context={'request': request})
class Books(APIView):
def get(self, request, *args, **kwargs):
ret = models.Book.objects.all()
# 生成一个序列化的对象,传参数
# 序列化多条,记住many=True
book_ser = BookSerializer(ret, many=True, context={'request': request})
print(book_ser.data)
return JsonResponse(book_ser.data, safe=False)
分享文章:HyperlinkedIdentityField
文章出自:http://ybzwz.com/article/iesiog.html