Search code examples
djangodjango-rest-frameworkdjango-guardian

How can control the items in view class queryset?


class ModelListCreateView(GetQuerysetMixin, generics.ListCreateAPIView):
    queryset = get_objects_for_user(self.context['request'].user, 'model.view_model')
    serializer_class = ModelSerializer
    permission_classes = (permissions.IsAuthenticated,)

In the above code snippet am trying to get query only the items a given user is supposed to see. I tried queryset = get_objects_for_user(request.user, 'model.view_model') but this did not work and resulted in an error 'request' not defined. After googling and trying what I have now I get the error 'self' not defined. And am also trying to implement django-guardian on top of the other permissions.


Solution

  • If you want to access the request object, then override get_queryset instead of setting queryset.

    class ModelListCreateView(GetQuerysetMixin, generics.ListCreateAPIView):
        def get_queryset(self):
            return get_objects_for_user(self.request.user, 'model.view_model')