diff options
| author | Tom Christie | 2012-12-12 20:44:55 +0000 |
|---|---|---|
| committer | Tom Christie | 2012-12-12 20:45:06 +0000 |
| commit | 497da7fc699b9e88c966e37bc48739865336683d (patch) | |
| tree | 57677cec1de1ab39f4561af8c647f27fe503941c /rest_framework/tests | |
| parent | 628e3bf001ca71da48a6f3c7bbdf209f2e20b223 (diff) | |
| download | django-rest-framework-497da7fc699b9e88c966e37bc48739865336683d.tar.bz2 | |
Clean up field initialization. Fixes #497
Diffstat (limited to 'rest_framework/tests')
| -rw-r--r-- | rest_framework/tests/serializer.py | 54 |
1 files changed, 53 insertions, 1 deletions
diff --git a/rest_framework/tests/serializer.py b/rest_framework/tests/serializer.py index 50a5f5a4..780177aa 100644 --- a/rest_framework/tests/serializer.py +++ b/rest_framework/tests/serializer.py @@ -4,7 +4,7 @@ from django.test import TestCase from rest_framework import serializers from rest_framework.tests.models import (Album, ActionItem, Anchor, BasicModel, BlankFieldModel, BlogPost, Book, CallableDefaultValueModel, DefaultValueModel, - ManyToManyModel, Person, ReadOnlyManyToManyModel) + ManyToManyModel, Person, ReadOnlyManyToManyModel, Photo) class SubComment(object): @@ -764,3 +764,55 @@ class DepthTest(TestCase): 'writer': {'id': 1, 'name': u'django', 'age': 1}} self.assertEqual(serializer.data, expected) + + +class NestedSerializerContextTests(TestCase): + + def test_nested_serializer_context(self): + """ + Regression for #497 + + https://github.com/tomchristie/django-rest-framework/issues/497 + """ + class PhotoSerializer(serializers.ModelSerializer): + class Meta: + model = Photo + fields = ("description", "callable") + + callable = serializers.SerializerMethodField('_callable') + + def _callable(self, instance): + if not 'context_item' in self.context: + raise RuntimeError("context isn't getting passed into 2nd level nested serializer") + return "success" + + class AlbumSerializer(serializers.ModelSerializer): + class Meta: + model = Album + fields = ("photo_set", "callable") + + photo_set = PhotoSerializer(source="photo_set") + callable = serializers.SerializerMethodField("_callable") + + def _callable(self, instance): + if not 'context_item' in self.context: + raise RuntimeError("context isn't getting passed into 1st level nested serializer") + return "success" + + class AlbumCollection(object): + albums = None + + class AlbumCollectionSerializer(serializers.Serializer): + albums = AlbumSerializer(source="albums") + + album1 = Album.objects.create(title="album 1") + album2 = Album.objects.create(title="album 2") + Photo.objects.create(description="Bigfoot", album=album1) + Photo.objects.create(description="Unicorn", album=album1) + Photo.objects.create(description="Yeti", album=album2) + Photo.objects.create(description="Sasquatch", album=album2) + album_collection = AlbumCollection() + album_collection.albums = [album1, album2] + + # This will raise RuntimeError if context doesn't get passed correctly to the nested Serializers + AlbumCollectionSerializer(album_collection, context={'context_item': 'album context'}).data |
