I have a pretty simple problem. I have a class that is extending APIView in order to pass in a Json Response to a URL which will be used as an API. I'm trying to filter data from a model so that I only get data items which belong to the current user. Because of this, I'm trying to access request.user.id to set my filter with.
From my views.py, here is a snippet of the code I am using:
class ChartData(APIView):
authentication_classes = []
permission_classes = []
def get(self, request, format=None):
print(self.request.user)
When I go to the url associated with this class, the print statement returns AnonymousUser, as if the user does not exist.
In the same views.py file I have another method (not inside any class) set up as the following:
@login_required
def LogDisplay(request):
print(request.user.id)
However, when I go to the url associated with this method, I get an integer as an output on my console.
How can I make it so that my first method has access to the current user as well?
You are close.
in settings.py:
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.IsAuthenticated',
)
}
in views.py:
from rest_framework.permissions import IsAuthenticated
class ChartData(APIView):
authentication_classes = []
permission_classes = [IsAuthenticated, ]
get(self, request, format=None):
...