mirror of
https://github.com/netbox-community/netbox.git
synced 2024-05-10 07:54:54 +00:00
Initial work on implementing django-taggit for #132
This commit is contained in:
@@ -13,7 +13,7 @@ from rest_framework.exceptions import APIException
|
||||
from rest_framework.permissions import BasePermission
|
||||
from rest_framework.relations import PrimaryKeyRelatedField
|
||||
from rest_framework.response import Response
|
||||
from rest_framework.serializers import Field, ModelSerializer, ValidationError
|
||||
from rest_framework.serializers import Field, ModelSerializer, RelatedField, ValidationError
|
||||
from rest_framework.viewsets import GenericViewSet, ViewSet
|
||||
|
||||
WRITE_OPERATIONS = ['create', 'update', 'partial_update', 'delete']
|
||||
@@ -42,6 +42,21 @@ class IsAuthenticatedOrLoginNotRequired(BasePermission):
|
||||
# Fields
|
||||
#
|
||||
|
||||
class TagField(RelatedField):
|
||||
"""
|
||||
Represent a writable list of Tags associated with an object (use with many=True).
|
||||
"""
|
||||
|
||||
def to_internal_value(self, data):
|
||||
obj = self.parent.parent.instance
|
||||
content_type = ContentType.objects.get_for_model(obj)
|
||||
tag, _ = Tag.objects.get_or_create(content_type=content_type, object_id=obj.pk, name=data)
|
||||
return tag
|
||||
|
||||
def to_representation(self, value):
|
||||
return value.name
|
||||
|
||||
|
||||
class ChoiceFieldSerializer(Field):
|
||||
"""
|
||||
Represent a ChoiceField as {'value': <DB value>, 'label': <string>}.
|
||||
|
Reference in New Issue
Block a user