Home > Mobile >  Get_queryset() missing 1 required positional argument: 'request' when i'm trying to f
Get_queryset() missing 1 required positional argument: 'request' when i'm trying to f

Time:04-22

I'm trying to list all facilties owned by a user on a "facility list page" using the django rest framework and react. I tried the following to make this happen (admin_uid is a one to one field of the user owning the facility):

class FacilityListView(ListAPIView):
    permission_classes = [AllowAny]
    serializer_class = FacilitySerializer
    

    def get_queryset(self, request):
        return Facility.objects.filter(admin_uid=self.request.user)

I'm getting this error:

django | queryset = self.filter_queryset(self.get_queryset()) django | TypeError: get_queryset() missing 1 required positional argument: 'request'

This is what i had before which worked but listed all facilities:

class FacilityListView(ListAPIView):
    permission_classes = [AllowAny]
    serializer_class = FacilitySerializer

    def get_queryset(self):
        return Facility.objects.all()

CodePudding user response:

You can do this

class FacilityListView(ListAPIView):
permission_classes = [AllowAny]
serializer_class = FacilitySerializer


def get_queryset(self, request):
    admin_uid = self.request.user.id
    return Facility.objects.filter(admin_uid=admin_uid)

CodePudding user response:

You will have to do 2 things:

First - get_queryset function takes only one parameter, self, so remove request from fn defination.

Second - To get logged in user detail add permission_class = [isAuthenticated]

For not logged in user you can create a different view with permission class = allowany, coz your filter won't work for not authenticated users.

  • Related