mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-04 20:40:14 +03:00
Merge f485099474
into 3110635685
This commit is contained in:
commit
f67b4b09b1
|
@ -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.
|
||||
|
||||
**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.
|
||||
- `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.
|
||||
|
||||
**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:
|
||||
|
||||
|
|
|
@ -1521,7 +1521,10 @@ class ListField(Field):
|
|||
}
|
||||
|
||||
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.max_length = kwargs.pop('max_length', None)
|
||||
self.min_length = kwargs.pop('min_length', None)
|
||||
|
@ -1582,7 +1585,10 @@ class DictField(Field):
|
|||
}
|
||||
|
||||
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 self.child.source is None, (
|
||||
|
|
|
@ -1744,6 +1744,13 @@ class TestUnvalidatedListField(FieldValues):
|
|||
field = serializers.ListField()
|
||||
|
||||
|
||||
class TestUnvalidatedListFieldWithChildNone(TestUnvalidatedListField):
|
||||
"""
|
||||
Values for `ListField` with `child=None`.
|
||||
"""
|
||||
field = serializers.ListField(child=None)
|
||||
|
||||
|
||||
class TestDictField(FieldValues):
|
||||
"""
|
||||
Values for `ListField` with CharField as child.
|
||||
|
@ -1809,6 +1816,13 @@ class TestUnvalidatedDictField(FieldValues):
|
|||
field = serializers.DictField()
|
||||
|
||||
|
||||
class TestUnvalidatedDictFieldWithChildNone(TestUnvalidatedDictField):
|
||||
"""
|
||||
Values for `ListField` with `child=None`.
|
||||
"""
|
||||
field = serializers.DictField(child=None)
|
||||
|
||||
|
||||
class TestJSONField(FieldValues):
|
||||
"""
|
||||
Values for `JSONField`.
|
||||
|
|
Loading…
Reference in New Issue
Block a user