您好,登錄后才能下訂單哦!
-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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。