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 <willem.vanonsem@prosafco.be>
This commit is contained in:
willeM_ Van Onsem 2024-02-20 15:08:19 +01:00 committed by GitHub
parent 37ec04d518
commit a45432b54d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -76,6 +76,7 @@ LIST_SERIALIZER_KWARGS = (
'instance', 'data', 'partial', 'context', 'allow_null', 'instance', 'data', 'partial', 'context', 'allow_null',
'max_length', 'min_length' 'max_length', 'min_length'
) )
LIST_SERIALIZER_KWARGS_REMOVE = ('allow_empty', 'min_length', 'max_length')
ALL_FIELDS = '__all__' ALL_FIELDS = '__all__'
@ -145,19 +146,12 @@ class BaseSerializer(Field):
kwargs['child'] = cls() kwargs['child'] = cls()
return CustomListSerializer(*args, **kwargs) return CustomListSerializer(*args, **kwargs)
""" """
allow_empty = kwargs.pop('allow_empty', None) list_kwargs = {}
max_length = kwargs.pop('max_length', None) for key in LIST_SERIALIZER_KWARGS_REMOVE:
min_length = kwargs.pop('min_length', None) value = kwargs.pop(key, None)
child_serializer = cls(*args, **kwargs) if value is not None:
list_kwargs = { list_kwargs[key] = value
'child': child_serializer, list_kwargs['child'] = cls(*args, **kwargs)
}
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.update({ list_kwargs.update({
key: value for key, value in kwargs.items() key: value for key, value in kwargs.items()
if key in LIST_SERIALIZER_KWARGS if key in LIST_SERIALIZER_KWARGS