This commit is contained in:
Arijeet Mukherjee 2017-08-15 13:33:20 +00:00 committed by GitHub
commit f67b4b09b1
3 changed files with 26 additions and 6 deletions

View File

@ -434,9 +434,9 @@ Requires either the `Pillow` package or `PIL` package. The `Pillow` package is
A field class that validates a list of objects. A field class that validates a list of objects.
**Signature**: `ListField(child, min_length=None, max_length=None)` **Signature**: `ListField(child=None, min_length=None, max_length=None)`
- `child` - A field instance that should be used for validating the objects in the list. If this argument is not provided then objects in the list will not be validated. - `child` - A field instance that should be used for validating the objects in the list. If this argument is not provided or is `None then objects in the list will not be validated.
- `min_length` - Validates that the list contains no fewer than this number of elements. - `min_length` - Validates that the list contains no fewer than this number of elements.
- `max_length` - Validates that the list contains no more than this number of elements. - `max_length` - Validates that the list contains no more than this number of elements.
@ -457,9 +457,9 @@ We can now reuse our custom `StringListField` class throughout our application,
A field class that validates a dictionary of objects. The keys in `DictField` are always assumed to be string values. A field class that validates a dictionary of objects. The keys in `DictField` are always assumed to be string values.
**Signature**: `DictField(child)` **Signature**: `DictField(child=None)`
- `child` - A field instance that should be used for validating the values in the dictionary. If this argument is not provided then values in the mapping will not be validated. - `child` - A field instance that should be used for validating the values in the dictionary. If this argument is not provided or is `None` then values in the mapping will not be validated.
For example, to create a field that validates a mapping of strings to strings, you would write something like this: For example, to create a field that validates a mapping of strings to strings, you would write something like this:

View File

@ -1521,7 +1521,10 @@ class ListField(Field):
} }
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
self.child = kwargs.pop('child', copy.deepcopy(self.child)) child = kwargs.pop('child', None)
if child is None:
child = copy.deepcopy(self.child)
self.child = child
self.allow_empty = kwargs.pop('allow_empty', True) self.allow_empty = kwargs.pop('allow_empty', True)
self.max_length = kwargs.pop('max_length', None) self.max_length = kwargs.pop('max_length', None)
self.min_length = kwargs.pop('min_length', None) self.min_length = kwargs.pop('min_length', None)
@ -1582,7 +1585,10 @@ class DictField(Field):
} }
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
self.child = kwargs.pop('child', copy.deepcopy(self.child)) child = kwargs.pop('child', None)
if child is None:
child = copy.deepcopy(self.child)
self.child = child
assert not inspect.isclass(self.child), '`child` has not been instantiated.' assert not inspect.isclass(self.child), '`child` has not been instantiated.'
assert self.child.source is None, ( assert self.child.source is None, (

View File

@ -1744,6 +1744,13 @@ class TestUnvalidatedListField(FieldValues):
field = serializers.ListField() field = serializers.ListField()
class TestUnvalidatedListFieldWithChildNone(TestUnvalidatedListField):
"""
Values for `ListField` with `child=None`.
"""
field = serializers.ListField(child=None)
class TestDictField(FieldValues): class TestDictField(FieldValues):
""" """
Values for `ListField` with CharField as child. Values for `ListField` with CharField as child.
@ -1809,6 +1816,13 @@ class TestUnvalidatedDictField(FieldValues):
field = serializers.DictField() field = serializers.DictField()
class TestUnvalidatedDictFieldWithChildNone(TestUnvalidatedDictField):
"""
Values for `ListField` with `child=None`.
"""
field = serializers.DictField(child=None)
class TestJSONField(FieldValues): class TestJSONField(FieldValues):
""" """
Values for `JSONField`. Values for `JSONField`.