Skip to content

Commit a45432b

Browse files
KommuSoftWillem Van Onsem
andauthored
refactor removing parameters from kwargs when creating a ListSerializer (#9245)
* refactor removing parameters from kwargs when creating a ListSerializer * insert child * small rewrite --------- Co-authored-by: Willem Van Onsem <[email protected]>
1 parent 37ec04d commit a45432b

File tree

1 file changed

+7
-13
lines changed

1 file changed

+7
-13
lines changed

rest_framework/serializers.py

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
'instance', 'data', 'partial', 'context', 'allow_null',
7777
'max_length', 'min_length'
7878
)
79+
LIST_SERIALIZER_KWARGS_REMOVE = ('allow_empty', 'min_length', 'max_length')
7980

8081
ALL_FIELDS = '__all__'
8182

@@ -145,19 +146,12 @@ def many_init(cls, *args, **kwargs):
145146
kwargs['child'] = cls()
146147
return CustomListSerializer(*args, **kwargs)
147148
"""
148-
allow_empty = kwargs.pop('allow_empty', None)
149-
max_length = kwargs.pop('max_length', None)
150-
min_length = kwargs.pop('min_length', None)
151-
child_serializer = cls(*args, **kwargs)
152-
list_kwargs = {
153-
'child': child_serializer,
154-
}
155-
if allow_empty is not None:
156-
list_kwargs['allow_empty'] = allow_empty
157-
if max_length is not None:
158-
list_kwargs['max_length'] = max_length
159-
if min_length is not None:
160-
list_kwargs['min_length'] = min_length
149+
list_kwargs = {}
150+
for key in LIST_SERIALIZER_KWARGS_REMOVE:
151+
value = kwargs.pop(key, None)
152+
if value is not None:
153+
list_kwargs[key] = value
154+
list_kwargs['child'] = cls(*args, **kwargs)
161155
list_kwargs.update({
162156
key: value for key, value in kwargs.items()
163157
if key in LIST_SERIALIZER_KWARGS

0 commit comments

Comments
 (0)