python - call other methods if django rest framwork's response code is 201 (succesfully created) -


i extend django views this

class userlist(apiview):     def post(self, etc, etc):         return response(serializer.data, status=status=status.http_201_created) 

now want call other methods if post reponse success. how can in same class userlist? (or have better idea?)

you can create postsuccessmixin class override dispatch() method. then, inherit mixin in our view , call super's dispatch(). on calling that, proper drf response. can check if status code of response 201. if 201, call other methods here. in end, return original drf response recieved after calling super's dispatch().

class postsuccessmixin(object):      def dispatch(self, request, *args, **kwargs):         response = super(postsuccessmixin, self).dispatch(request, *args, **kwargs)         if response.status_code == 201:             ...             call other methods             ...         return response 

in views, inherit mixin. views.py

class userlist(postsuccessmixin, apiview):     def post(self, etc, etc):         return response(serializer.data, status=status=status.http_201_created) 

Comments