Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

319
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda