Hi
I need to perform a pre_save check prior to updating a model. The check requires access to the session user, so I thought that the perform_update method on ModelViewSet would be a good place for the code.
My code is as follows:
def perform_update(self, serializer):
user = self.request.user
order = self.get_object()
if user.is_customer and order.status not in (Order.STATUS.unallocated, Order.STATUS.authorised,):
return Response(status=status.HTTP_403_FORBIDDEN)
serializer.save()
The code executed as expected, however, a 403 is not returned? Can't see why? I've looked at the code in UpdateModelMixin and there is nothing to suggest why this wouldn't work
However, if I override the actual update method, I can throw a 403. I guess the fact that serializer.is_valid() has not thrown an exception, is the reason, though I would expect to be able to return an HTTP Response of my liking at any point?
What am I missing? Is there a better place to validate the user against the model? I guess I could add a method to the serializer to set the session user, and modify the update method to check against this, hence throwing a validation error.. though this is more a security check
thnx