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

312
Visualizações
how to overwrite RetrieveAPIView response django rest framework

I am fetching data from RetrieveAPIView I want to overwrite it.

class PostDetailAPIView(RetrieveAPIView):
    queryset = Post.objects.all()
    serializer_class = PostDetailSerializer
    lookup_field = 'slug'

http://127.0.0.1:8000/api/posts/post-python/

it return me result

{
    "id": 2,
    "title": "python",
    "slug": "post-python",
    "content": "content of python"
}

I want to overwrite this with some extra parameters like

[ 
   'result':
   {
    "id": 2,
    "title": "python",
    "slug": "post-python",
    "content": "content of python"
    },
   'message':'success'
]
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Ok, in comment I made wrong call, you want to overwrite get() method of your View.

class PostDetailAPIView(RetrieveAPIView):
    queryset = Post.objects.all()
    serializer_class = PostDetailSerializer
    lookup_field = 'slug'

    def get(self, request, slug):
        post = self.get_object(slug)
        serializer = PostDetailSerializer(post)
        return Response({
            'result': serializer.data,
            'message': 'success'
        })

Notes 1. I names second argument of get function slug because it's your lookup_field, but it should be name you used in urls. 2. You could instead overwrite retrieve() function 3. This is answer specific for your question, you should also read this answer

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