fix(validators): guard UniqueConstraint validators against queryset instance in ListSerializer (fixes #9484) #10000
All Vouch review confidence checks passed
Residual risk 0.17 = Change Risk (0.43) × (1 − Review Confidence (0.60)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.43 · 391 lines diff across 4 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.357
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #10000"
general
weight: 0.35
p-r-a-v-i-n
Problem
Fixes #9484 —
AttributeError: 'BaseQuerySet' object has no attribute 'pk'when usingModelSerializerwithmany=Trueon a model that has aUniqueConstraint.Root Cause
When
many=Trueis passed to aModelSerializer,many_initcreates aListSerializerwrapping the child serializer. Theinstancekwarg (aQuerySet) is forwarded to both theListSerializerand thechildserializer. When the child'sUniqueTogetherValidator(orUniqueValidator) runs during validation, it calls:Because
instanceis the entireQuerySet, not a model instance,.pkraisesAttributeError.Reproducer (from #9484)
Fix
Introduces a small private helper
_is_single_instance(instance)that returnsTrueonly ifinstanceis a non-None, non-QuerySet, non-list object that has a.pkattribute. BothUniqueValidator.exclude_current_instanceandUniqueTogetherValidator.exclude_current_instance(andBaseUniqueForValidator.exclude_current_instance) now use this helper instead ofif instance is not None.This is minimal and fully backward-compatible:
instance.pkstill used to exclude the current object.instance): validators no longer crash; treat each child item as a create.Noneinstance (plain create): unchanged.The fix also corrects
UniqueTogetherValidator.filter_querysetand__call__which had the same latent bug of reading fromserializer.instancewithout guarding against a queryset type.Checklist
UniqueValidator,UniqueTogetherValidator, andBaseUniqueForValidatorfilter_querysetpath inUniqueTogetherValidator(latent bug:getattr(serializer.instance, source)would also crash if instance is a queryset)