retrieve data for current user only in DRF

1.2k Views Asked by At

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.

1

There are 1 best solutions below

0
MisterNox On BEST ANSWER

This should actually work by overriding the get_queryset method. Simply add this method to your ListAPIView and it should work.

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.