Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

304
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!