您好,登录后才能下订单哦!
-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<pky>\d+)', views.Publish.as_view(),name='ttt'),
-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)
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。