api.py 690 B

123456789101112131415161718192021
  1. from rest_framework.exceptions import APIException
  2. from rest_framework.serializers import ModelSerializer
  3. WRITE_OPERATIONS = ['create', 'update', 'partial_update', 'delete']
  4. class ServiceUnavailable(APIException):
  5. status_code = 503
  6. default_detail = "Service temporarily unavailable, please try again later."
  7. class WritableSerializerMixin(object):
  8. """
  9. Allow for the use of an alternate, writable serializer class for write operations (e.g. POST, PUT).
  10. """
  11. def get_serializer_class(self):
  12. if self.action in WRITE_OPERATIONS and hasattr(self, 'write_serializer_class'):
  13. return self.write_serializer_class
  14. return self.serializer_class