mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-04 12:30:11 +03:00
Merge 2ef3cb8b68
into 21074a02b4
This commit is contained in:
commit
27ff17a18b
|
@ -563,10 +563,10 @@ class ListSerializer(BaseSerializer):
|
|||
assert self.child is not None, '`child` is a required argument.'
|
||||
assert not inspect.isclass(self.child), '`child` has not been instantiated.'
|
||||
super(ListSerializer, self).__init__(*args, **kwargs)
|
||||
self.child.bind(field_name='', parent=self)
|
||||
|
||||
def bind(self, field_name, parent):
|
||||
super(ListSerializer, self).bind(field_name, parent)
|
||||
self.child.bind(field_name='', parent=self)
|
||||
self.partial = self.parent.partial
|
||||
|
||||
def get_initial(self):
|
||||
|
|
62
tests/test_root.py
Normal file
62
tests/test_root.py
Normal file
|
@ -0,0 +1,62 @@
|
|||
from __future__ import unicode_literals
|
||||
|
||||
from django.test import TestCase, override_settings
|
||||
|
||||
from rest_framework import serializers
|
||||
|
||||
|
||||
class ChildSerializer(serializers.Serializer):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(self, *args, **kwargs)
|
||||
# Not calling self.root
|
||||
|
||||
def to_internal_value(self, data):
|
||||
value = super().to_internal_value(data)
|
||||
value['root'] = self.root
|
||||
return value
|
||||
|
||||
|
||||
class AnotherChildSerializer(serializers.Serializer):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(self, *args, **kwargs)
|
||||
self.root # Calling self.root to ruin the cache
|
||||
|
||||
def to_internal_value(self, data):
|
||||
value = super().to_internal_value(data)
|
||||
value['root'] = self.root
|
||||
return value
|
||||
|
||||
|
||||
class ParentSerializer(serializers.Serializer):
|
||||
children = ChildSerializer(many=True)
|
||||
other_children = AnotherChildSerializer(many=True)
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(self, *args, **kwargs)
|
||||
# Not calling self.root
|
||||
|
||||
def to_internal_value(self, data):
|
||||
value = super().to_internal_value(data)
|
||||
value['root'] = self.root
|
||||
return value
|
||||
|
||||
|
||||
class RootSerializer(serializers.Serializer):
|
||||
parents = ParentSerializer(many=True)
|
||||
|
||||
|
||||
class ListManyToManyTests(TestCase):
|
||||
def setUp(self):
|
||||
self.root = RootSerializer(data={
|
||||
'parents': [{
|
||||
'children': [{'name': 'child'}],
|
||||
'other_children': [{'name': 'child'}],
|
||||
}],
|
||||
})
|
||||
self.root.is_valid()
|
||||
|
||||
def test_relative_hyperlinks(self):
|
||||
assert self.root.root == self.root
|
||||
assert self.root.validated_data['parents'][0]['root'] == self.root
|
||||
assert self.root.validated_data['parents'][0]['children'][0]['root'] == self.root
|
||||
assert self.root.validated_data['parents'][0]['other_children'][0]['root'] == self.root
|
Loading…
Reference in New Issue
Block a user