From 7166a78520c527ef107a85d91b39823ed7643e49 Mon Sep 17 00:00:00 2001 From: shrajal01 Date: Mon, 10 Nov 2025 22:15:58 +0530 Subject: [PATCH 1/4] Refactor get_initial for list input handlingFix ListSerializer.get_initial to return consistent initial list structure Modify get_initial to return a consistent list format when initial_data is a list. --- rest_framework/serializers.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py index ea2daffd5a..ac7e39892c 100644 --- a/rest_framework/serializers.py +++ b/rest_framework/serializers.py @@ -608,10 +608,15 @@ def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.child.bind(field_name='', parent=self) - def get_initial(self): - if hasattr(self, 'initial_data'): - return self.to_representation(self.initial_data) + def get_initial(self): + if hasattr(self, 'initial_data'): + # If data is given, we should just return the raw input structure, + # but ensure it's represented in a consistent list format. + if isinstance(self.initial_data, list): + return [self.child.get_initial() for _ in self.initial_data] return [] + return [] + def get_value(self, dictionary): """ From 8fa867aeb62048a449bff5782b8279dc9bc5c7b9 Mon Sep 17 00:00:00 2001 From: shrajal01 Date: Wed, 12 Nov 2025 01:55:52 +0530 Subject: [PATCH 2/4] Fix indentation and implementation of get_initial in ListSerializer Corrected the get_initial method to return consistent list format and fixed indentation to avoid SyntaxError / IndentationError in tests. --- rest_framework/serializers.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py index ac7e39892c..8834cb173b 100644 --- a/rest_framework/serializers.py +++ b/rest_framework/serializers.py @@ -608,14 +608,17 @@ def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.child.bind(field_name='', parent=self) - def get_initial(self): - if hasattr(self, 'initial_data'): - # If data is given, we should just return the raw input structure, - # but ensure it's represented in a consistent list format. - if isinstance(self.initial_data, list): - return [self.child.get_initial() for _ in self.initial_data] + def get_initial(self): + """ + Return a list of initial values, one for each item in `initial_data`, + or an empty list if no input data was provided. + """ + if hasattr(self, 'initial_data'): + if isinstance(self.initial_data, list): + return [self.child.get_initial() for _ in self.initial_data] + return [] return [] - return [] + def get_value(self, dictionary): From 3e19d6a3acdda4e91b5da9c31ad44a865769a006 Mon Sep 17 00:00:00 2001 From: shrajal01 Date: Wed, 12 Nov 2025 02:05:30 +0530 Subject: [PATCH 3/4] Remove unnecessary blank lines in serializers.py --- rest_framework/serializers.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py index 8834cb173b..48c34dabf1 100644 --- a/rest_framework/serializers.py +++ b/rest_framework/serializers.py @@ -597,7 +597,7 @@ class ListSerializer(BaseSerializer): 'max_length': _('Ensure this field has no more than {max_length} elements.'), 'min_length': _('Ensure this field has at least {min_length} elements.') } - + def __init__(self, *args, **kwargs): self.child = kwargs.pop('child', copy.deepcopy(self.child)) self.allow_empty = kwargs.pop('allow_empty', True) @@ -618,9 +618,7 @@ def get_initial(self): return [self.child.get_initial() for _ in self.initial_data] return [] return [] - - - + def get_value(self, dictionary): """ Given the input dictionary, return the field value. From dc1e9d2a9530d73f52b060bcc14c5b510831b1d2 Mon Sep 17 00:00:00 2001 From: shrajal01 Date: Wed, 12 Nov 2025 02:26:07 +0530 Subject: [PATCH 4/4] Update serializers.py