mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-23 17:08:41 -06:00
Update serializer context assignment for DRF 3.11
This commit is contained in:
parent
8c7b3a1f9b
commit
a914a7c438
@ -20,7 +20,10 @@ class CustomFieldDefaultValues:
|
|||||||
"""
|
"""
|
||||||
Return a dictionary of all CustomFields assigned to the parent model and their default values.
|
Return a dictionary of all CustomFields assigned to the parent model and their default values.
|
||||||
"""
|
"""
|
||||||
def __call__(self):
|
requires_context = True
|
||||||
|
|
||||||
|
def __call__(self, serializer_field):
|
||||||
|
self.model = serializer_field.parent.Meta.model
|
||||||
|
|
||||||
# Retrieve the CustomFields for the parent model
|
# Retrieve the CustomFields for the parent model
|
||||||
content_type = ContentType.objects.get_for_model(self.model)
|
content_type = ContentType.objects.get_for_model(self.model)
|
||||||
@ -49,9 +52,6 @@ class CustomFieldDefaultValues:
|
|||||||
|
|
||||||
return value
|
return value
|
||||||
|
|
||||||
def set_context(self, serializer_field):
|
|
||||||
self.model = serializer_field.parent.Meta.model
|
|
||||||
|
|
||||||
|
|
||||||
class CustomFieldsSerializer(serializers.BaseSerializer):
|
class CustomFieldsSerializer(serializers.BaseSerializer):
|
||||||
|
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
from rest_framework import serializers
|
from rest_framework import serializers
|
||||||
from rest_framework.validators import UniqueTogetherValidator
|
|
||||||
from taggit_serializer.serializers import TaggitSerializer, TagListSerializerField
|
from taggit_serializer.serializers import TaggitSerializer, TagListSerializerField
|
||||||
|
|
||||||
from dcim.api.nested_serializers import NestedDeviceSerializer
|
from dcim.api.nested_serializers import NestedDeviceSerializer
|
||||||
@ -43,13 +42,6 @@ class SecretSerializer(TaggitSerializer, CustomFieldModelSerializer):
|
|||||||
data['ciphertext'] = s.ciphertext
|
data['ciphertext'] = s.ciphertext
|
||||||
data['hash'] = s.hash
|
data['hash'] = s.hash
|
||||||
|
|
||||||
# Validate uniqueness of name if one has been provided.
|
|
||||||
if data.get('name'):
|
|
||||||
validator = UniqueTogetherValidator(queryset=Secret.objects.all(), fields=('device', 'role', 'name'))
|
|
||||||
validator.set_context(self)
|
|
||||||
validator(data)
|
|
||||||
|
|
||||||
# Enforce model validation
|
|
||||||
super().validate(data)
|
super().validate(data)
|
||||||
|
|
||||||
return data
|
return data
|
||||||
|
Loading…
Reference in New Issue
Block a user