Add tags in article API responses

parent 04bcd303
Pipeline #1205 passed with stages
in 1 minute and 57 seconds
from rest_framework import serializers
from taggit.managers import TaggableManager
from rp.models import Article
class TagListSerializer(serializers.Field):
class Meta:
model = TaggableManager(blank=True)
def to_representation(self, obj):
if type(obj) is not list:
return [tag.name for tag in obj.all()]
return obj
class ArticleSerializer(serializers.ModelSerializer):
tags = TagListSerializer()
class Meta:
model = Article
fields = ('id', 'url', 'title', 'extracts', 'und_score_up', 'und_score_down')
fields = ('id', 'url', 'title', 'tags', 'extracts', 'und_score_up', 'und_score_down')
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment