Skip to content

Commit 7166a78

Browse files
authored
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.
1 parent 365d409 commit 7166a78

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

rest_framework/serializers.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -608,10 +608,15 @@ def __init__(self, *args, **kwargs):
608608
super().__init__(*args, **kwargs)
609609
self.child.bind(field_name='', parent=self)
610610

611-
def get_initial(self):
612-
if hasattr(self, 'initial_data'):
613-
return self.to_representation(self.initial_data)
611+
def get_initial(self):
612+
if hasattr(self, 'initial_data'):
613+
# If data is given, we should just return the raw input structure,
614+
# but ensure it's represented in a consistent list format.
615+
if isinstance(self.initial_data, list):
616+
return [self.child.get_initial() for _ in self.initial_data]
614617
return []
618+
return []
619+
615620

616621
def get_value(self, dictionary):
617622
"""

0 commit comments

Comments
 (0)