[Django]-Django rest framework – Pass id of newly created object back with response after POST

2👍

Although this is an old question as I came across the same problem and found the answer I though I’d share it. You should use this line in your serializer:

id = serializers.ReadOnlyField()
👤Amen

1👍

If you use the generic views a “post” function is provided to, and it looks by default like

def post(self, request, *args, **kwargs):
    return self.create(request, *args, **kwargs) # A Response object with the data you saved

You could instead return pretty much anything you want:

def post(self, request, *args, **kwargs):
    self.create(request, *args, **kwargs)
    job = # Fetch the object you just created and serialize it
    return Response(job)
👤pingul

Leave a comment