mirror of
				https://github.com/encode/django-rest-framework.git
				synced 2025-11-04 09:57:55 +03:00 
			
		
		
		
	Merge pull request #3300 from jpadilla/listfield-child
Raise error when `source=` use on a child.
This commit is contained in:
		
						commit
						ed65db367a
					
				| 
						 | 
				
			
			@ -1381,7 +1381,13 @@ class ListField(Field):
 | 
			
		|||
    def __init__(self, *args, **kwargs):
 | 
			
		||||
        self.child = kwargs.pop('child', copy.deepcopy(self.child))
 | 
			
		||||
        self.allow_empty = kwargs.pop('allow_empty', True)
 | 
			
		||||
 | 
			
		||||
        assert not inspect.isclass(self.child), '`child` has not been instantiated.'
 | 
			
		||||
        assert self.child.source is None, (
 | 
			
		||||
            "The `source` argument is not meaningful when applied to a `child=` field. "
 | 
			
		||||
            "Remove `source=` from the field declaration."
 | 
			
		||||
        )
 | 
			
		||||
 | 
			
		||||
        super(ListField, self).__init__(*args, **kwargs)
 | 
			
		||||
        self.child.bind(field_name='', parent=self)
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -1424,7 +1430,13 @@ class DictField(Field):
 | 
			
		|||
 | 
			
		||||
    def __init__(self, *args, **kwargs):
 | 
			
		||||
        self.child = kwargs.pop('child', copy.deepcopy(self.child))
 | 
			
		||||
 | 
			
		||||
        assert not inspect.isclass(self.child), '`child` has not been instantiated.'
 | 
			
		||||
        assert self.child.source is None, (
 | 
			
		||||
            "The `source` argument is not meaningful when applied to a `child=` field. "
 | 
			
		||||
            "Remove `source=` from the field declaration."
 | 
			
		||||
        )
 | 
			
		||||
 | 
			
		||||
        super(DictField, self).__init__(*args, **kwargs)
 | 
			
		||||
        self.child.bind(field_name='', parent=self)
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1416,6 +1416,15 @@ class TestListField(FieldValues):
 | 
			
		|||
    ]
 | 
			
		||||
    field = serializers.ListField(child=serializers.IntegerField())
 | 
			
		||||
 | 
			
		||||
    def test_no_source_on_child(self):
 | 
			
		||||
        with pytest.raises(AssertionError) as exc_info:
 | 
			
		||||
            serializers.ListField(child=serializers.IntegerField(source='other'))
 | 
			
		||||
 | 
			
		||||
        assert str(exc_info.value) == (
 | 
			
		||||
            "The `source` argument is not meaningful when applied to a `child=` field. "
 | 
			
		||||
            "Remove `source=` from the field declaration."
 | 
			
		||||
        )
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class TestEmptyListField(FieldValues):
 | 
			
		||||
    """
 | 
			
		||||
| 
						 | 
				
			
			@ -1461,6 +1470,15 @@ class TestDictField(FieldValues):
 | 
			
		|||
    ]
 | 
			
		||||
    field = serializers.DictField(child=serializers.CharField())
 | 
			
		||||
 | 
			
		||||
    def test_no_source_on_child(self):
 | 
			
		||||
        with pytest.raises(AssertionError) as exc_info:
 | 
			
		||||
            serializers.DictField(child=serializers.CharField(source='other'))
 | 
			
		||||
 | 
			
		||||
        assert str(exc_info.value) == (
 | 
			
		||||
            "The `source` argument is not meaningful when applied to a `child=` field. "
 | 
			
		||||
            "Remove `source=` from the field declaration."
 | 
			
		||||
        )
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class TestUnvalidatedDictField(FieldValues):
 | 
			
		||||
    """
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue
	
	Block a user