From 8fa867aeb62048a449bff5782b8279dc9bc5c7b9 Mon Sep 17 00:00:00 2001 From: shrajal01 Date: Wed, 12 Nov 2025 01:55:52 +0530 Subject: [PATCH] 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 ac7e39892..8834cb173 100644 --- a/rest_framework/serializers.py +++ b/rest_framework/serializers.py @@ -608,14 +608,17 @@ class ListSerializer(BaseSerializer): 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):