Trying to override a get_queryset in a view

1.1k views Asked by At
class OwnerList(generics.ListAPIView):

      serializer_class = OwnerDetailSerializer
      # queryset = Person.objects.filter(customuser__userrole__role__name='OWNER').distinct()

      permission_classes = [IsAuthenticated]

      filter_backends = [DjangoFilterBackend]

      def get_queryset(self):
           return super(OwnerList, self).get_queryset()

I have this simple view and i am trying to over ride the get_queryset. The issue is that when this view is used i get :

return super(OwnerList, self).get_queryset()

File "C:\Users\kdalipaj\PycharmProjects\LTC SYSTEM\venv\lib\site-packages\rest_framework\generics.py", line 63, in get_queryset assert self.queryset is not None, (

AssertionError: 'OwnerList' should either include a queryset attribute, or override the get_queryset() method.

Why is this happening?

1

There are 1 answers

0
MihanEntalpo On

Your code is:

return super(OwnerList, self).get_queryset()

it means: "Call method get_queryset of ListAPIView.

ListAPIView doesn't have get_queryset method itself but GenericAPIView (which is a parent of ListAPIView) has one.

And get_queryset of the GenericAPIView doing simple thing: it throwing assertion.

So, you shouldn't call

return super(OwnerList, self).get_queryset()

Just create your own queryset and return it from the function. For example:

return Owner.objects

Another solution is to set queryset variable at a class-level:

class OwnerList(generics.ListAPIView):
    ...
    queryset = Owner.objects
    ...