Related Queryset filter

14 views
Skip to first unread message

Malik Brahimi

unread,
Feb 26, 2020, 3:22:12 PM2/26/20
to Django users
class GridViewset(ModelViewSet):
    queryset = Grid.objects.all()
    serializer_class = GridSerial


class ItemViewset(ModelViewSet):
    queryset = Item.objects.all()
    serializer_class = ItemSerial

    def get_queryset(self):
        if self.request.user.is_staff:
            return Item.objects.all()
        return Item.objects.filter(scope = "C")


So I have a Grid model which represents an table on my frontend and an Item model which represents a row of that table via a foreign key. I'd like to filter the items which all have a scope field to limit which user can view it such that staff users can see everything, and other users will be restricted. That being said there are potentially tables with no items at all. If the user is_staff I'd like to show the table, otherwise filter the grids by those whose item queryset length will be at least on item. Is there a way to kinda precomute this related queryset from the related viewset. 
Reply all
Reply to author
Forward
0 new messages