[英]retrieve data for current user only in DRF
I was wondering how can I limit the queryset to the data associated with the current user.我想知道如何将查询集限制为与当前用户关联的数据。 Here is my view:
这是我的看法:
class TradingGroupList(generics.ListAPIView):
queryset = Tradegroup.objects.all()
serializer_class = TradeGroupSerializer
name = 'tradegroup-list'
I would write somethinng like queryset = Tradegroup.objects.filter(owner=self.request.user)
in native django, but was wondering how I could achieve this here.我会在本机 django 中写一些类似于
queryset = Tradegroup.objects.filter(owner=self.request.user)
的东西,但想知道如何在这里实现这一点。
This should actually work by overriding the get_queryset method.这实际上应该通过覆盖 get_queryset 方法来工作。 Simply add this method to your ListAPIView and it should work.
只需将此方法添加到您的 ListAPIView 中,它应该可以工作。
class TradingGroupList(generics.ListAPIView):
serializer_class = TradeGroupSerializer
name = 'tradegroup-list'
def get_queryset(self)
return Tradegroup.objects.all().filter(owner=self.request.user)
I hope this works for you.我希望这对你有用。 If you have a problem with it just give me a comment.
如果您对此有任何问题,请给我评论。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.