Fix ListSerializer.get_initial to return consistent initial list structure#9822
Fix ListSerializer.get_initial to return consistent initial list structure#9822shrajal01 wants to merge 4 commits intoencode:mainfrom
Conversation
…itial to return consistent initial list structure Modify get_initial to return a consistent list format when initial_data is a list.
Corrected the get_initial method to return consistent list format and fixed indentation to avoid SyntaxError / IndentationError in tests.
There was a problem hiding this comment.
Pull Request Overview
This PR fixes ListSerializer.get_initial() to return a consistent initial list structure instead of using to_representation(), which could cause incorrect input→output transformations when rendering form-like serializers.
Key changes:
- Updated
get_initial()to return a list of initial values from child serializers wheninitial_datais provided - Added proper docstring explaining the method's behavior
- Removed the call to
to_representation()that was causing inconsistent behavior
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| return self.to_representation(self.initial_data) | ||
| if isinstance(self.initial_data, list): | ||
| return [self.child.get_initial() for _ in self.initial_data] | ||
| return [] |
There was a problem hiding this comment.
The return [] statement on line 620 is unreachable code. When hasattr(self, 'initial_data') is True, the code returns either on line 618 (if initial_data is a list) or on line 619 (if it's not a list). When the condition is False, execution jumps directly to line 620.
This results in redundant code since line 619 and line 620 both return the same empty list.
Suggested fix - remove line 619 and keep only line 620:
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 [] |
auvipy
left a comment
There was a problem hiding this comment.
please add reference issue in the description section and appropriate unit tests for verifying the changes
peterthomassen
left a comment
There was a problem hiding this comment.
Thanks. I've added a comment where I think there's a problem.
Also, please add a test to demonstrate before/after behavior, and undo the unneeded whitespace changes. Thanks!
| if hasattr(self, 'initial_data'): | ||
| return self.to_representation(self.initial_data) | ||
| if isinstance(self.initial_data, list): | ||
| return [self.child.get_initial() for _ in self.initial_data] |
There was a problem hiding this comment.
This does not seem correct, as all the list items would be the same.
This change updates
ListSerializer.get_initial()to return an initiallist structure when
initial_datais provided. The previous behaviordid not correctly handle list initialization and could result in
inconsistent initial states when rendering form-like serializers.
The updated implementation ensures:
initial_datais a list, an initial list structure is returnedto_representation()This aligns ListSerializer behavior with Serializer.get_initial and
prevents incorrect input→output transformations.