from rest_framework import serializers from taggit_serializer.serializers import TagListSerializerField, TaggitSerializer from .models import Note, Notebook, LuxTag import six class NewTagListSerializerField(TagListSerializerField): def to_internal_value(self, value): if isinstance(value, six.string_types): value = [v.strip() for v in value.split(',')] if not isinstance(value, list): self.fail('not_a_list', input_type=type(value).__name__) for s in value: if not isinstance(s, six.string_types): self.fail('not_a_str') self.child.run_validation(s) return value class NoteSerializer(TaggitSerializer, serializers.ModelSerializer): tags = NewTagListSerializerField(required=False) class Meta: model = Note fields = ('id', 'title', 'body_text', 'body_qjson', 'body_html', 'url', 'notebook', 'tags') class NotebookSerializer(serializers.HyperlinkedModelSerializer): json_absolute_url = serializers.URLField(source='get_absolute_url', read_only=True) owner = serializers.HiddenField( default=serializers.CurrentUserDefault() ) class Meta: model = Notebook fields = ('id', 'name', 'color_rgb', 'json_absolute_url', 'owner') class NoteTagSerializer(serializers.HyperlinkedModelSerializer): class Meta: model = LuxTag fields = ('name', 'color_hex')