-
-
Notifications
You must be signed in to change notification settings - Fork 7.1k
Fix validation for ListSerializer when many=True #9774
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?
Changes from 2 commits
c3a8ad9
33b4977
395cebe
7ef2f1d
44c3552
abb9320
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -652,6 +652,26 @@ def run_child_validation(self, data): | |||||||||||||||||||||||||||||||||||||||
| self.child.initial_data = data | ||||||||||||||||||||||||||||||||||||||||
| return super().run_child_validation(data) | ||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||
| child_instance = getattr(self.child, "instance", None) | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| if self.instance is not None: | ||||||||||||||||||||||||||||||||||||||||
| pk_name = None | ||||||||||||||||||||||||||||||||||||||||
| child_meta = getattr(self.child, "Meta", None) | ||||||||||||||||||||||||||||||||||||||||
| model = getattr(child_meta, "model", None) if child_meta else None | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| if model is not None: | ||||||||||||||||||||||||||||||||||||||||
| pk_name = model._meta.pk.name | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| if pk_name: | ||||||||||||||||||||||||||||||||||||||||
| obj_id = data.get(pk_name, data.get("pk", data.get("id"))) | ||||||||||||||||||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can see plenty of ways to break this: what if the PK is a UUID field called
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That’s true, but the same is already true for single updates. If the PK is serialized under a different name (e.g. uuid, uid, etc.), DRF can’t resolve it automatically there either unless the serializer is customized. If anything works for single updates, it will also work for bulk updates, the constraints are the same.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I disagree, with a single instance, you have the instance and the data, so it's a 1 to 1 mapping and you know it should match. With a list of dicts on one hand and a list of instances/queryset on the other, you need to map which dict corresponds to which instance. This mapping will depend on the use case, and needs a unique identifier somewhere (which could be anything: PK, email, slug, combination of fields...). Hence why users need to do it, DRF can't do it for them.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see your point about single updates having a direct instance - data mapping. But I’d still argue the difference is one of quantity rather than fundamentals. Even in single updates, DRF assumes that the mapping is correct only because the caller provided the right instance. If the serializer is misaligned (e.g. PK serialized under another field, or a different uniqueness condition like email/slug), DRF doesn’t solve that, the user has to customize the serializer. For bulk updates, the requirement is the same: there needs to be some unique identifier to match instance ↔ data. Whether that identifier is pk, uuid, email, or something else, the logic isn’t different from single updates, just applied across a list. So I don’t see it as “DRF can’t do it at all,” but more that DRF could apply the same assumptions it already makes in the single case, and users who need different identifiers would still override/customize. |
||||||||||||||||||||||||||||||||||||||||
| if obj_id is not None: | ||||||||||||||||||||||||||||||||||||||||
| for obj in self.instance: | ||||||||||||||||||||||||||||||||||||||||
| if hasattr(obj, pk_name) and getattr(obj, pk_name) == obj_id: | ||||||||||||||||||||||||||||||||||||||||
| child_instance = obj | ||||||||||||||||||||||||||||||||||||||||
| break | ||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+667
to
+671
|
||||||||||||||||||||||||||||||||||||||||
| for field_name, field in self.child.fields.items(): | |
| if getattr(field, "source", None) == pk_name: | |
| obj_id = data.get(field_name) | |
| if obj_id is not None: | |
| break | |
| # Cache the mapping from pk_name to field_name for efficiency | |
| if not hasattr(self, "_pk_field_name_cache"): | |
| self._pk_field_name_cache = {} | |
| cache_key = (self.child.__class__, pk_name) | |
| field_name = self._pk_field_name_cache.get(cache_key) | |
| if field_name is None: | |
| for fname, field in self.child.fields.items(): | |
| if getattr(field, "source", None) == pk_name: | |
| field_name = fname | |
| self._pk_field_name_cache[cache_key] = field_name | |
| break | |
| obj_id = None | |
| if field_name is not None: | |
| obj_id = data.get(field_name) |
Copilot
AI
Dec 14, 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.
There's no test coverage for serializers without a Meta.model (non-ModelSerializer) when using many=True with instances. The code checks if model is None at line 662 and 692, but no tests verify this path works correctly when using plain Serializers instead of ModelSerializers.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -150,3 +150,30 @@ def __new__(cls, *args, **kwargs): | |
| help_text='OneToOneTarget', | ||
| verbose_name='OneToOneTarget', | ||
| on_delete=models.CASCADE) | ||
|
|
||
|
|
||
| class ListModelForTest(RESTFrameworkModel): | ||
| name = models.CharField(max_length=100) | ||
| status = models.CharField(max_length=100, blank=True) | ||
|
|
||
| @property | ||
| def is_valid(self): | ||
| return self.name == 'valid' | ||
|
Comment on lines
+159
to
+161
|
||
|
|
||
|
|
||
| class EmailPKModel(RESTFrameworkModel): | ||
| email = models.EmailField(primary_key=True) | ||
| name = models.CharField(max_length=100) | ||
|
|
||
| @property | ||
| def is_valid(self): | ||
| return self.name == 'valid' | ||
|
Comment on lines
+168
to
+170
|
||
|
|
||
|
|
||
| class PersonUUID(RESTFrameworkModel): | ||
| id = models.UUIDField(primary_key=True) | ||
| name = models.CharField(max_length=100) | ||
|
|
||
| @property | ||
| def is_valid(self): | ||
| return self.name == 'valid' | ||
|
Comment on lines
+177
to
+179
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,7 +5,8 @@ | |
| from rest_framework import serializers | ||
| from rest_framework.exceptions import ErrorDetail | ||
| from tests.models import ( | ||
| CustomManagerModel, NullableOneToOneSource, OneToOneTarget | ||
| CustomManagerModel, EmailPKModel, ListModelForTest, NullableOneToOneSource, | ||
| OneToOneTarget, PersonUUID | ||
| ) | ||
|
|
||
|
|
||
|
|
@@ -775,3 +776,93 @@ def test(self): | |
| queryset = NullableOneToOneSource.objects.all() | ||
| serializer = self.serializer(queryset, many=True) | ||
| assert serializer.data | ||
|
|
||
|
|
||
| @pytest.mark.django_db | ||
| class TestManyTrueValidationCheck: | ||
| """ | ||
| Tests ListSerializer validation with many=True across different primary key types | ||
| (integer and email). | ||
| """ | ||
|
|
||
| def setup_method(self): | ||
| self.obj1 = ListModelForTest.objects.create(name="valid", status="new") | ||
| self.obj2 = ListModelForTest.objects.create(name="invalid", status="") | ||
| self.email_obj1 = EmailPKModel.objects.create(email="test@test.com", name="A") | ||
| self.email_obj2 = EmailPKModel.objects.create(email="test2@test.com", name="B") | ||
|
|
||
| self.serializer, self.email_serializer = self.get_serializers() | ||
|
|
||
| def get_serializers(self): | ||
| class ListModelForTestSerializer(serializers.ModelSerializer): | ||
| class Meta: | ||
| model = ListModelForTest | ||
| fields = ("id", "name", "status") | ||
|
|
||
| def validate_status(self, value): | ||
| if value and not self.instance.is_valid: | ||
| return False | ||
| return value | ||
|
Comment on lines
+815
to
+818
|
||
|
|
||
| class EmailPKSerializer(serializers.ModelSerializer): | ||
| class Meta: | ||
| model = EmailPKModel | ||
| fields = ("email", "name") | ||
| read_only_fields = ('email',) | ||
|
|
||
| def validate_name(self, value): | ||
| if value and not self.instance.is_valid: | ||
| return False | ||
| return value | ||
|
Comment on lines
+826
to
+829
|
||
|
|
||
| return ListModelForTestSerializer, EmailPKSerializer | ||
|
|
||
| def test_run_child_validation_with_many_true(self): | ||
| input_data = [ | ||
| {"id": self.obj1.pk, "name": "other", "status": "new"}, | ||
| {"id": self.obj2.pk, "name": "valid", "status": "progress"}, | ||
| ] | ||
|
|
||
| serializer = self.serializer([self.obj1, self.obj2], data=input_data, many=True) | ||
| assert serializer.is_valid(), serializer.errors | ||
|
|
||
| serializer = self.serializer(ListModelForTest.objects.all(), data=input_data, many=True) | ||
| assert serializer.is_valid(), serializer.errors | ||
|
|
||
| def test_validation_error_for_invalid_data(self): | ||
| input_data = [{"id": self.obj1.pk, "name": "", "status": "mystatus"}] | ||
|
|
||
| serializer = self.serializer([self.obj1], data=input_data, many=True) | ||
| assert not serializer.is_valid() | ||
| assert "name" in serializer.errors[0] | ||
|
|
||
| def test_email_pk_instance_validation(self): | ||
| input_data = [{"email": "test@test.com", "name": "bar"}] | ||
| serializer = self.email_serializer(instance=EmailPKModel.objects.all(), data=input_data, many=True) | ||
| assert serializer.is_valid(), serializer.errors | ||
|
|
||
| def test_uuid_validate_many(self): | ||
| PersonUUID.objects.create(id="c20f2f31-65a3-451f-ae7d-e939b7d9f84b", name="valid") | ||
| PersonUUID.objects.create(id="3308237e-18d8-4074-9d05-79cc0fdb5bb3", name="other") | ||
|
|
||
| class PersonUUIDSerializer(serializers.ModelSerializer): | ||
| uuid = serializers.UUIDField(source="id") | ||
|
|
||
| class Meta: | ||
| model = PersonUUID | ||
| fields = ("uuid", "name") | ||
| read_only_fields = ('uuid',) | ||
|
|
||
| def validate_name(self, value): | ||
| if value and not self.instance.is_valid: | ||
| return False | ||
| return value | ||
|
|
||
| input_data = [ | ||
| { | ||
| "uuid": "t3308237e-18d8-4074-9d05-79cc0fdb5bb3", | ||
| "name": "bar", | ||
| }, | ||
| ] | ||
|
Comment on lines
+861
to
+866
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this example is a bit overestimated. If the model’s primary key is id, it’s not a common or practical scenario to remap it to uuid and then expect DRF to resolve it automatically during updates. In this setup, even single-object updates wouldn’t work without extra customization, since the serializer no longer exposes the real PK field. That’s not a limitation of bulk updates, it’s a limitation of how the serializer is defined. So I don’t think this example shows a specific weakness of many=True updates. |
||
| serializer = PersonUUIDSerializer(instance=list(PersonUUID.objects.all()), data=input_data, many=True) | ||
| assert serializer.is_valid(), serializer.errors | ||
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 docstring example shows calling super().run_child_validation(data), but the actual implementation at line 701 calls self.child.run_validation(data) directly. This makes the example misleading since there's no parent class implementation being called. The example should be updated to match the actual implementation pattern or clarify that this is an example override.