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 def to_internal_value(self, data): return [tag.strip() for tag in data.split(",")] class ArticleSerializer(serializers.ModelSerializer): #: List of short tags to describe the article (eg. "Privacy", "Copyright") tags = TagListSerializer(help_text=""" List of short tags to describe the article (eg."Privacy, Copyright"). Must be a list of tags, coma separated (or an empty string). """) und_score_up = serializers.IntegerField( required=False, help_text="This is used to increase the vote count by this value") und_score_down = serializers.IntegerField( required=False, help_text="This is used to decrease the vote count by this value") und_score = serializers.IntegerField( required=False, help_text="This is the actual computed score for an Article") class Meta: model = Article fields = ('id', 'url', 'title', 'tags', 'extracts', 'status', 'und_score_up', 'und_score_down', 'und_score') def create(self, validated_data): article = Article.add_new_url(**validated_data) article.save() return article