django rest framework perform_update or post_save return custom response -
how can return custom response json error when object older 8 minutes.
here code, response doesn't work:
def perform_update(self, serializer): instance = serializer.save() diff_minutes = (datetime.now() - instance.ordered_date).total_seconds() / 60 if diff_minutes > 8: return response({'expired': true}, status=status.http_500_internal_server_error) self.post_save(instance)
same when use post_save
.
i don't response, pass.
any suggestion?
since want return 500 error can raise apiexception
. example:
from rest_framework.exceptions import apiexception def perform_update(self, serializer): instance = serializer.save() diff_minutes = (datetime.now() - instance.ordered_date).total_seconds() / 60 if diff_minutes > 8: raise apiexception("expired") self.post_save(instance)
raising apiexception
trigger .handle_exception(self, exc)
function. default return result like:
{detail : "expired" }
and status code of 500
. if need more control on exact return value / status code can overwrite handle_exception
function , return whatever like.
also since apparently don't have enough rep comment, question previous posted solution stating want validation on update. can overwrite update()
function in serializer , called on update opposed create()
called when create new object.
Comments
Post a Comment