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 使用起来真的很方便。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值