Skip to content

Conversation

@zainnadeem786
Copy link

Summary

This PR fixes issue #8926 by updating ListSerializer to preserve and provide access to self.instance during validation when many=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 using id or pk.

Key Enhancements

  1. Automated Instance Matching

    • ListSerializer.run_child_validation now attempts to match input data to items in self.instance.
    • Builds an instance map for O(1) lookup during validation.
    • Supports subclasses that may override instance assignments.
  2. Validation Fixes

    • Avoided premature access to validated_data by returning run_validation results directly.
    • Manually restores instance and initial_data in deepcopied child serializers.
    • Partial updates (partial=True) correctly propagate from root serializer to list items.
    • Standardized error reporting in to_internal_value for positional list errors.
  3. Test Suite Updates

    • Updated 37 tests in tests/test_serializer_lists.py to reflect consistent validation and instance matching behavior.
    • Added regression test test_many_true_regression_8926 to confirm that validate_<field> methods can now access self.instance during bulk updates.

Verification

  • Ran pytest tests/test_serializer_lists.pyall 37 tests passed.
  • Confirmed that individual list items now correctly reference their associated instance during validation.
  • Verified correct handling of allow_empty, min_length, max_length, and nested serializers.

Notes

  • This PR does not change the public API of ListSerializer.
  • It improves reliability and consistency for serializers using many=True, particularly for update operations.

Related Issues

@zainnadeem786 zainnadeem786 reopened this Jan 25, 2026
@zainnadeem786 zainnadeem786 force-pushed the improve-many-true-validation-guidance branch from d42540b to c205e9f Compare January 25, 2026 18:22
@zainnadeem786 zainnadeem786 force-pushed the improve-many-true-validation-guidance branch from c205e9f to f0375ca Compare January 25, 2026 18:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant