serializers.py 583 Bytes
Newer Older
luxcem's avatar
luxcem committed
1 2
from rest_framework import serializers

3
from taggit.managers import TaggableManager
luxcem's avatar
luxcem committed
4 5
from rp.models import Article

6 7 8 9 10 11 12 13
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
luxcem's avatar
luxcem committed
14 15

class ArticleSerializer(serializers.ModelSerializer):
16 17
    tags = TagListSerializer()

luxcem's avatar
luxcem committed
18 19
    class Meta:
        model = Article
20
        fields = ('id', 'url', 'title', 'tags', 'extracts', 'und_score_up', 'und_score_down')