-
-
Notifications
You must be signed in to change notification settings - Fork 7k
Fix ListSerializer.get_initial to return consistent initial list structure #9822
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 [] |
Copilot
AI
Nov 12, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.