Django-rest framwork RetrieveModelMixin单个数据查询

在浏览器中输入网址http://120.79.84.147:3389/show/TV/6/可以访问获取单一信息的接口(id为6的数据):
Django-rest framwork RetrieveModelMixin单个数据查询_第1张图片

这个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 framwork RetrieveModelMixin单个数据查询_第2张图片
不得不说,django rest framework 使用起来真的很方便。

你可能感兴趣的:(django,rest,framework,python)