mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-04 12:30:11 +03:00
Fix Issue #5301. Fix docs + allow None to be passed in to child
This commit is contained in:
parent
83c535e9cc
commit
edad462ca3
|
@ -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:
|
||||
|
||||
|
|
|
@ -1522,7 +1522,10 @@ class ListField(Field):
|
|||
}
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
self.child = kwargs.pop('child', copy.deepcopy(self.child))
|
||||
child = kwargs.pop('child')
|
||||
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)
|
||||
|
@ -1583,7 +1586,10 @@ class DictField(Field):
|
|||
}
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
self.child = kwargs.pop('child', copy.deepcopy(self.child))
|
||||
child = kwargs.pop('child')
|
||||
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, (
|
||||
|
|
Loading…
Reference in New Issue
Block a user