Commit f6491a65 authored by Okhin's avatar Okhin
Browse files

We're nowusing a nested tag serializer for the API

parent 7d8b06e1
from rest_framework import serializers
from taggit.managers import TaggableManager
from taggit.models import Tag
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 [ for tag in obj.all()]
return obj
class TagListSerializer(serializers.ModelSerializer):
name = serializers.CharField(max_length=200)
def to_internal_value(self, data):
return [tag.strip() for tag in data.split(",")]
class Meta:
model = Tag
fields = ('name', )
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).
""", default="")
List of short tags to describe the article (eg."Privacy", "Copyright").
It must be a valid JSON list of items with a field named name.
For instance [{"name": "Privacy"}, {"name": "Copyright"}]
""", many=True, required=False)
class Meta:
model = Article
......@@ -158,7 +158,7 @@ class TestArticleApi(TestCase):
{'url': a.url, 'title': a.title,
'tags': ''
assert [ for t in a.tags.all()] ==['tags']
assert [ for t in a.tags.all()] == [t['name'] for t in['tags']]
def test_api_recover(self):
# Can we recover if we're no Jedis
Supports Markdown
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