Django-rest framwork RetrieveModelMixin单个数据查询

  • 时间:
  • 来源:互联网
  • 文章标签:

在浏览器中输入网址http://120.79.84.147:3389/show/TV/6/可以访问获取单一信息的接口(id为6的数据):
在这里插入图片描述

这个id为主键由字段lookup_field来决定
看看源码:

class GenericAPIView(views.APIView):
	pass
    # If you want to use object lookups other than pk, set 'lookup_field'.
    # For more complex lookup requirements override `get_object()`.
    lookup_field = 'pk'
    lookup_url_kwarg = None

那么如果想输入url:http://120.79.84.147:3389/show/TV/6/返回account_id=6的数据怎么办呢?
加一句:lookup_field = "account_id"即可:

class TestViewSet(mixins.RetrieveModelMixin,
                  mixins.DestroyModelMixin,
                  mixins.ListModelMixin,
                  viewsets.GenericViewSet):
    queryset = LoginPrison.objects.all()
    serializer_class = LoginPrisonSerializer
    pagination_class = LimitOffsetPagination
    lookup_field = "account_id"


不得不说,django rest framework 使用起来真的很方便。

本文链接http://www.taodudu.cc/news/show-647572.html