Home > Blockchain >  ListCreateAPIView doesn't return the response
ListCreateAPIView doesn't return the response

Time:03-25

I want to return the custom json response after perform_create

class MyObjList(generics.ListCreateAPIView):
    def get_serializer_class(self):
        if self.request.method == 'GET':
            return MyObjListSerializer
        return MyObjCreateSerializer
    def perform_create(self, serializer):
        // do something here. 
        return Response({'isDraft': 1})
class MyObjCreateSerializer(serializers.Serializer):
    class Meta:
        fields = ('answer')

However this dosen't return the any response,for my Javasctipt

axios.post(API_URL, data, config).then(({ data }) => {
  console.log(data); // just show {}
}).catch(error => {
  console.log(error);
});

What I want to do is create object in perform_create and return the keyname to the client.

However currently in my code it doesn't return anything.

I wonder if Serializer class is relevant, but I am not sure the role of serializer class for POST>

CodePudding user response:

You shoul override the create() method instead of perform_create() method. Here is explained why: https://stackoverflow.com/a/69395096/8332763

  • perform_create changes saving logic.
  • create changes request/response handling
  • Related