glemmaPaul / django-taggit-serializer

The Django Taggit Serializer Created for the Django REST Framework

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Not working with Django==1.8.2 and djangorestframework==3.1.3

thenewguy opened this issue · comments

This isn't working for me. I added the field to my serializer as shown in the readme. With it I cannot add tags via the api. Also, if I update a record with the api that already had tags, the tags are cleared.

Not sure if it is relevant, but I had to use a custom tag model per taggit docs since I use UUID primary keys.

class TaggedFoo(TaggedItemBase):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    content_object = models.ForeignKey('Foo')

class Foo(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    tags = TaggableManager(through=TaggedFoo)

This field is working but transfers the tags as a json string instead of a list.

class TagJSONListField(serializers.Field):
    def to_internal_value(self, value):
        if isinstance(value, basestring):
            if not value:
                value = "[]"
            value = json.loads(value)

        if not isinstance(value, list):
            raise exceptions.ValidationError("Invalid JSON List <{}>".format(value))

        return value

    def to_representation(self, value):
        if not isinstance(value, basestring):
            if not isinstance(value, list):
                value = [tag.name for tag in value.all()]
            value = json.dumps(value)

        return value

Just posting for reference until I can investigate further