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

240
Visualizações
Custom permissions in Django

In Django Rest framework, we can verify permissions such as (isAuthenticated, isAdminUser...) but how can we add our custom permissions and decide what django can do with those permissions?

I really want to understand what happens behind (I didn't find a documentation that explaint this):

@permission_classes([IsAdminUser])

Thank you

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

0

Write your own permissions, like this:

def permission_valid_token(func):
    # first arg is the viewset, second is the request
    def wrapper(*args, **kwargs):
        valid_token, user_token = test_token_authorization(args[1].headers) # my function to validate the user
        if not valid_token:
            return Response(status=status.HTTP_401_UNAUTHORIZED)
        return func(*args, **kwargs)
    return wrapper

This is a permission that i'm using in app, probably you will have to change the valid_token part

And them you import in your views

from file_that_you_use.permissions import permission_valid_token

And you use as a decorator

class WeatherViewSet(viewsets.ViewSet):
    @permission_valid_token
    def list(self, request):

This is just a viewset for example, you can use generic viewsets or whatever you want.

over 4 years ago · Santiago Trujillo Relatório

0

If you are using VSCode, hover over @permission_classes([IsAdminUser]), click on Command (on your keyboard).

enter image description here

You can see what happens behind the scenes, play and create your custom Django version (not recommended) or you can overwrite the function.

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