aboutsummaryrefslogtreecommitdiffstats
path: root/rest_framework/tests/test_genericrelations.py
diff options
context:
space:
mode:
authorXavier Ordoquy2014-04-13 00:05:57 +0200
committerXavier Ordoquy2014-04-13 00:05:57 +0200
commitd08536ad9d026fb7126c430f6d9c18f8540aacd6 (patch)
treea8a1d36ce76867e57da23379694ea0609801990b /rest_framework/tests/test_genericrelations.py
parent2911cd64ad67ba193e3d37322ee71692cb482623 (diff)
parent93b9245b8714287a440023451ff7880a2f6e5b32 (diff)
downloaddjango-rest-framework-d08536ad9d026fb7126c430f6d9c18f8540aacd6.tar.bz2
Merge remote-tracking branch 'origin/master' into 2.4.0
Conflicts: .travis.yml docs/api-guide/fields.md docs/api-guide/routers.md docs/topics/release-notes.md rest_framework/authentication.py rest_framework/serializers.py rest_framework/templatetags/rest_framework.py rest_framework/tests/test_authentication.py rest_framework/tests/test_filters.py rest_framework/tests/test_hyperlinkedserializers.py rest_framework/tests/test_serializer.py rest_framework/tests/test_testing.py rest_framework/utils/encoders.py tox.ini
Diffstat (limited to 'rest_framework/tests/test_genericrelations.py')
-rw-r--r--rest_framework/tests/test_genericrelations.py39
1 files changed, 36 insertions, 3 deletions
diff --git a/rest_framework/tests/test_genericrelations.py b/rest_framework/tests/test_genericrelations.py
index c38bfb9f..fa09c9e6 100644
--- a/rest_framework/tests/test_genericrelations.py
+++ b/rest_framework/tests/test_genericrelations.py
@@ -4,8 +4,10 @@ from django.contrib.contenttypes.generic import GenericRelation, GenericForeignK
from django.db import models
from django.test import TestCase
from rest_framework import serializers
+from rest_framework.compat import python_2_unicode_compatible
+@python_2_unicode_compatible
class Tag(models.Model):
"""
Tags have a descriptive slug, and are attached to an arbitrary object.
@@ -15,10 +17,11 @@ class Tag(models.Model):
object_id = models.PositiveIntegerField()
tagged_item = GenericForeignKey('content_type', 'object_id')
- def __unicode__(self):
+ def __str__(self):
return self.tag
+@python_2_unicode_compatible
class Bookmark(models.Model):
"""
A URL bookmark that may have multiple tags attached.
@@ -26,10 +29,11 @@ class Bookmark(models.Model):
url = models.URLField()
tags = GenericRelation(Tag)
- def __unicode__(self):
+ def __str__(self):
return 'Bookmark: %s' % self.url
+@python_2_unicode_compatible
class Note(models.Model):
"""
A textual note that may have multiple tags attached.
@@ -37,7 +41,7 @@ class Note(models.Model):
text = models.TextField()
tags = GenericRelation(Tag)
- def __unicode__(self):
+ def __str__(self):
return 'Note: %s' % self.text
@@ -69,6 +73,35 @@ class TestGenericRelations(TestCase):
}
self.assertEqual(serializer.data, expected)
+ def test_generic_nested_relation(self):
+ """
+ Test saving a GenericRelation field via a nested serializer.
+ """
+
+ class TagSerializer(serializers.ModelSerializer):
+ class Meta:
+ model = Tag
+ exclude = ('content_type', 'object_id')
+
+ class BookmarkSerializer(serializers.ModelSerializer):
+ tags = TagSerializer()
+
+ class Meta:
+ model = Bookmark
+ exclude = ('id',)
+
+ data = {
+ 'url': 'https://docs.djangoproject.com/',
+ 'tags': [
+ {'tag': 'contenttypes'},
+ {'tag': 'genericrelations'},
+ ]
+ }
+ serializer = BookmarkSerializer(data=data)
+ self.assertTrue(serializer.is_valid())
+ serializer.save()
+ self.assertEqual(serializer.object.tags.count(), 2)
+
def test_generic_fk(self):
"""
Test a relationship that spans a GenericForeignKey field.