From 7166a78520c527ef107a85d91b39823ed7643e49 Mon Sep 17 00:00:00 2001 From: shrajal01 Date: Mon, 10 Nov 2025 22:15:58 +0530 Subject: [PATCH] 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 ea2daffd5..ac7e39892 100644 --- a/rest_framework/serializers.py +++ b/rest_framework/serializers.py @@ -608,10 +608,15 @@ class ListSerializer(BaseSerializer): 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): """