Wednesday, February 26, 2020

Related Queryset filter

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. 

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/aba871ee-fc07-42a3-b6d7-c1b2c11aab6a%40googlegroups.com.

No comments:

Post a Comment