mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-26 19:43:59 +03:00
Merge pull request #802 from chenjyw/master
Fix nesting issue with depth >=2
This commit is contained in:
commit
50873343b2
|
@ -205,18 +205,6 @@ class BaseSerializer(WritableField):
|
|||
|
||||
return ret
|
||||
|
||||
#####
|
||||
# Field methods - used when the serializer class is itself used as a field.
|
||||
|
||||
def initialize(self, parent, field_name):
|
||||
"""
|
||||
Same behaviour as usual Field, except that we need to keep track
|
||||
of state so that we can deal with handling maximum depth.
|
||||
"""
|
||||
super(BaseSerializer, self).initialize(parent, field_name)
|
||||
if parent.opts.depth:
|
||||
self.opts.depth = parent.opts.depth - 1
|
||||
|
||||
#####
|
||||
# Methods to convert or revert from objects <--> primitive representations.
|
||||
|
||||
|
@ -619,6 +607,8 @@ class ModelSerializer(Serializer):
|
|||
class NestedModelSerializer(ModelSerializer):
|
||||
class Meta:
|
||||
model = model_field.rel.to
|
||||
depth = self.opts.depth - 1
|
||||
|
||||
return NestedModelSerializer()
|
||||
|
||||
def get_related_field(self, model_field, to_many=False):
|
||||
|
|
|
@ -3,7 +3,7 @@ from django.utils.datastructures import MultiValueDict
|
|||
from django.test import TestCase
|
||||
from rest_framework import serializers
|
||||
from rest_framework.tests.models import (HasPositiveIntegerAsChoice, Album, ActionItem, Anchor, BasicModel,
|
||||
BlankFieldModel, BlogPost, Book, CallableDefaultValueModel, DefaultValueModel,
|
||||
BlankFieldModel, BlogPost, BlogPostComment, Book, CallableDefaultValueModel, DefaultValueModel,
|
||||
ManyToManyModel, Person, ReadOnlyManyToManyModel, Photo)
|
||||
import datetime
|
||||
import pickle
|
||||
|
@ -767,8 +767,6 @@ class RelatedTraversalTest(TestCase):
|
|||
post = BlogPost.objects.create(title="Test blog post", writer=user)
|
||||
post.blogpostcomment_set.create(text="I love this blog post")
|
||||
|
||||
from rest_framework.tests.models import BlogPostComment
|
||||
|
||||
class PersonSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = Person
|
||||
|
@ -968,23 +966,26 @@ class SerializerPickleTests(TestCase):
|
|||
|
||||
class DepthTest(TestCase):
|
||||
def test_implicit_nesting(self):
|
||||
|
||||
writer = Person.objects.create(name="django", age=1)
|
||||
post = BlogPost.objects.create(title="Test blog post", writer=writer)
|
||||
comment = BlogPostComment.objects.create(text="Test blog post comment", blog_post=post)
|
||||
|
||||
class BlogPostSerializer(serializers.ModelSerializer):
|
||||
class BlogPostCommentSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = BlogPost
|
||||
depth = 1
|
||||
model = BlogPostComment
|
||||
depth = 2
|
||||
|
||||
serializer = BlogPostSerializer(instance=post)
|
||||
expected = {'id': 1, 'title': 'Test blog post',
|
||||
'writer': {'id': 1, 'name': 'django', 'age': 1}}
|
||||
serializer = BlogPostCommentSerializer(instance=comment)
|
||||
expected = {'id': 1, 'text': 'Test blog post comment', 'blog_post': {'id': 1, 'title': 'Test blog post',
|
||||
'writer': {'id': 1, 'name': 'django', 'age': 1}}}
|
||||
|
||||
self.assertEqual(serializer.data, expected)
|
||||
|
||||
def test_explicit_nesting(self):
|
||||
writer = Person.objects.create(name="django", age=1)
|
||||
post = BlogPost.objects.create(title="Test blog post", writer=writer)
|
||||
comment = BlogPostComment.objects.create(text="Test blog post comment", blog_post=post)
|
||||
|
||||
class PersonSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
|
@ -996,9 +997,15 @@ class DepthTest(TestCase):
|
|||
class Meta:
|
||||
model = BlogPost
|
||||
|
||||
serializer = BlogPostSerializer(instance=post)
|
||||
expected = {'id': 1, 'title': 'Test blog post',
|
||||
'writer': {'id': 1, 'name': 'django', 'age': 1}}
|
||||
class BlogPostCommentSerializer(serializers.ModelSerializer):
|
||||
blog_post = BlogPostSerializer()
|
||||
|
||||
class Meta:
|
||||
model = BlogPostComment
|
||||
|
||||
serializer = BlogPostCommentSerializer(instance=comment)
|
||||
expected = {'id': 1, 'text': 'Test blog post comment', 'blog_post': {'id': 1, 'title': 'Test blog post',
|
||||
'writer': {'id': 1, 'name': 'django', 'age': 1}}}
|
||||
|
||||
self.assertEqual(serializer.data, expected)
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user