1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00

Introduced WritableSerializerMixin

This commit is contained in:
Jeremy Stretch
2017-01-27 14:36:13 -05:00
parent fa900d5dbb
commit 12d263999b
6 changed files with 68 additions and 37 deletions

View File

@@ -1,6 +1,30 @@
from rest_framework.exceptions import APIException
from rest_framework.serializers import ModelSerializer
WRITE_OPERATIONS = ['create', 'update', 'partial_update', 'delete']
class ServiceUnavailable(APIException):
status_code = 503
default_detail = "Service temporarily unavailable, please try again later."
class WritableSerializerMixin(object):
"""
Returns a flat Serializer from the given model suitable for write operations (POST, PUT, PATCH). This is necessary
to allow write operations on objects which utilize nested serializers.
"""
def get_serializer_class(self):
class WritableSerializer(ModelSerializer):
class Meta:
model = self.queryset.model
fields = '__all__'
if self.action in WRITE_OPERATIONS:
return WritableSerializer
return self.serializer_class