简体   繁体   中英

How to using django-taggit in DRF

I am having trouble to serialize the tags from django-taggit. I followed the instruction from here , but it is outdated.

Here is what I did:

views.py

class TagsSerializer(serializers.WritableField):

    def from_native(self, data):
        if type(data) is not list:
            raise ParseError("expected a list of data")
        return data

    def to_native(self, obj):
        if type(obj) is not list:
            return [tag.name for tag in obj.all()]
        return obj

I got this error :

'module' object has no attribute 'WritableField

Apparently the WritableField is deprecated.

I am using django 1.8, DRF 3.2 and django-taggit-0.17.

I would use the TaggableManager to update the tags, with a custom ListField to handle the serialisation of tags. Then you can use the serializer create/update methods to set the tags.

serializers.py

class TagSerializerField(serializers.ListField):
    child = serializers.CharField()

    def to_representation(self, data):
        return data.values_list('name', flat=True)

class TagSerializer(serializers.ModelSerializer):
    tags = TagSerializerField()

    def create(self, validated_data):
        tags = validated_data.pop('tags')
        instance = super(TagSerializer, self).create(validated_data)
        instance.tags.set(*tags)
        return instance

or you can use the perform_create/perform_update hooks in the view.

views.py

class TagView(APIView):
    queryset = Tag.objects.all()
    serializer_class = TagSerializer

    def perform_create(self, serializer):
        instance = serializer.save()
        if 'tags' in self.request.DATA:
            instance.tags.set(*self.request.DATA['tags'])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM