Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

317
Visualizações
Django current user is AnonymousUser

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?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

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):
    ...
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda