'Django RF, error validating objects owner (error: Field 'id' expected a number but got .AnonymousUser )

I have a object created by a user, When other users try to access the object, it should not allow it. I expect Django to throw error as you are not authenticated After logging out, when anonymous user try to access the object Django throw error as mentioned below.

ERROR

TypeError at /PoUserTracking/
Field 'id' expected a number but got <django.contrib.auth.models.AnonymousUser object at 0x0445F160>.

VIEWS.PY

class PoUserTracking(generics.ListAPIView):
    serializer_class = NewPoSerializer
    permission_classes = [IsAuthor]

    def get_queryset(self):
        user = self.request.user
        return Po.objects.filter(user=user)

PERMISSIONS.PY

class IsAuthor(permissions.BasePermission):

    def has_object_permission(self, request, view, obj):
        
        return obj.author == request.user


Solution 1:[1]

just make request.user.id //this will be solved for sure..

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Bharath Kumar