Fix #8926: ListSerializer supports instance access during validation for many=True #9879
+198
−113
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR fixes issue #8926 by updating
ListSerializerto preserve and provide access toself.instanceduring validation whenmany=True. Previously, child serializers in bulk updates could not access their corresponding instance, causing AssertionErrors and inconsistent behavior. This update ensures that each item in a list serializer automatically matches its input data to the correct instance usingidorpk.Key Enhancements
Automated Instance Matching
ListSerializer.run_child_validationnow attempts to match input data to items inself.instance.instanceassignments.Validation Fixes
validated_databy returningrun_validationresults directly.instanceandinitial_datain deepcopied child serializers.partial=True) correctly propagate from root serializer to list items.to_internal_valuefor positional list errors.Test Suite Updates
tests/test_serializer_lists.pyto reflect consistent validation and instance matching behavior.test_many_true_regression_8926to confirm thatvalidate_<field>methods can now accessself.instanceduring bulk updates.Verification
pytest tests/test_serializer_lists.py→ all 37 tests passed.allow_empty,min_length,max_length, and nested serializers.Notes
ListSerializer.many=True, particularly for update operations.Related Issues
self.instancewhen validating the serializer usingmany=True#8926