From a45432b54de88b28bd2e6db31acfe3dbcfc748dd Mon Sep 17 00:00:00 2001 From: willeM_ Van Onsem <3482343+KommuSoft@users.noreply.github.com> Date: Tue, 20 Feb 2024 15:08:19 +0100 Subject: [PATCH] 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 --- rest_framework/serializers.py | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py index 77c181b6c..eee18ddd4 100644 --- a/rest_framework/serializers.py +++ b/rest_framework/serializers.py @@ -76,6 +76,7 @@ LIST_SERIALIZER_KWARGS = ( 'instance', 'data', 'partial', 'context', 'allow_null', 'max_length', 'min_length' ) +LIST_SERIALIZER_KWARGS_REMOVE = ('allow_empty', 'min_length', 'max_length') ALL_FIELDS = '__all__' @@ -145,19 +146,12 @@ class BaseSerializer(Field): kwargs['child'] = cls() return CustomListSerializer(*args, **kwargs) """ - allow_empty = kwargs.pop('allow_empty', None) - max_length = kwargs.pop('max_length', None) - min_length = kwargs.pop('min_length', None) - child_serializer = cls(*args, **kwargs) - list_kwargs = { - 'child': child_serializer, - } - if allow_empty is not None: - list_kwargs['allow_empty'] = allow_empty - if max_length is not None: - list_kwargs['max_length'] = max_length - if min_length is not None: - list_kwargs['min_length'] = min_length + list_kwargs = {} + for key in LIST_SERIALIZER_KWARGS_REMOVE: + value = kwargs.pop(key, None) + if value is not None: + list_kwargs[key] = value + list_kwargs['child'] = cls(*args, **kwargs) list_kwargs.update({ key: value for key, value in kwargs.items() if key in LIST_SERIALIZER_KWARGS