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:
@@ -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
|
||||
|
Reference in New Issue
Block a user