Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Open
p-r-a-v-i-n wants to merge 4 commits into encode:main
base: main
Choose a base branch
Loading
from p-r-a-v-i-n:fix/bulk-validation-serializer

Conversation

@p-r-a-v-i-n
Copy link
Contributor

@p-r-a-v-i-n p-r-a-v-i-n commented Sep 1, 2025

Added tests and ensured that ListSerializer properly validates multiple objects when many=True.
Covers both valid and invalid data scenarios.

Changes include:

  • Tests for validating multiple instances when many=True was passed.
  • Tests for handling invalid data in a list
  • Simplified ListSerializer usage in tests without custom run_child_validation

This aligns with the ongoing Django REST Framework issue #8926

@p-r-a-v-i-n p-r-a-v-i-n force-pushed the fix/bulk-validation-serializer branch from aec3126 to 5502965 Compare September 1, 2025 14:38
instance = None
if self.instance is not None:
instance_map = {getattr(obj, 'pk', None): obj for obj in self.instance}
obj_id = data.get('id') or data.get('pk')
Copy link
Collaborator

@browniebroke browniebroke Sep 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line assumes that the primary key field is called "id" or "pk" but it could be called anything and DRF can't know it in advance so this fix will work in a limited subset of cases.

Copy link
Contributor Author

@p-r-a-v-i-n p-r-a-v-i-n Sep 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are correct , but in the existing TestListSerializerContainingNestedSerializer class was using hardcoded 'pk' keys.
But your argument make sense and i have updated the it.

Copy link
Collaborator

@browniebroke browniebroke Sep 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The difference with TestListSerializerContainingNestedSerializer is that this is demonstrating something that would be implemented by the consumers of DRF, showing how one would override run_child_validation to match up the instance with the provided data, which is highly dependant on the use case.

Here, you're doing that in the library code.

Comment on lines 790 to 805
class TestListModelSerializer(serializers.ModelSerializer):
class Meta:
model = TestListModel
fields = ("id", "name", "status")

def validate_status(self, value):
if value and not self.instance.is_valid:
return False
return value
Copy link
Collaborator

@browniebroke browniebroke Sep 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor: would be nice to not duplicate this serializer in each test function (see the other classes above for some examples on how to do that).

@p-r-a-v-i-n p-r-a-v-i-n force-pushed the fix/bulk-validation-serializer branch from 5502965 to 06ebf29 Compare September 2, 2025 05:56
@p-r-a-v-i-n p-r-a-v-i-n force-pushed the fix/bulk-validation-serializer branch from 06ebf29 to c3a8ad9 Compare September 2, 2025 06:03
pk_name = model._meta.pk.name

if pk_name:
obj_id = data.get(pk_name, data.get("pk", data.get("id")))
Copy link
Collaborator

@browniebroke browniebroke Sep 2, 2025

Choose a reason for hiding this comment

The 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 id but serialized as uuid? Sometimes it's called uid... Are we going to handle all possible field name people are using in the wild?

auvipy reacted with thumbs up emoji
Copy link
Contributor Author

@p-r-a-v-i-n p-r-a-v-i-n Sep 3, 2025

Choose a reason for hiding this comment

The 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.

Copy link
Collaborator

@browniebroke browniebroke Sep 3, 2025

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

@p-r-a-v-i-n p-r-a-v-i-n Sep 3, 2025

Choose a reason for hiding this comment

The 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.

Comment on lines 861 to 866
input_data = [
{
"uuid": "t3308237e-18d8-4074-9d05-79cc0fdb5bb3",
"name": "bar",
},
]
Copy link
Contributor Author

@p-r-a-v-i-n p-r-a-v-i-n Sep 3, 2025

Choose a reason for hiding this comment

The 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.

read_only_fields = ("uuid",)

def validate_name(self, value):
print('SELF-INSTANCe: ', self.instance)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'm not sure about the print here

Copy link
Contributor Author

@p-r-a-v-i-n p-r-a-v-i-n Sep 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh sorry, I mistakenly left after debugging.

@p-r-a-v-i-n p-r-a-v-i-n force-pushed the fix/bulk-validation-serializer branch from 87a7ea5 to 7ef2f1d Compare September 4, 2025 10:13
Copy link
Contributor Author

p-r-a-v-i-n commented Oct 12, 2025
edited
Loading

Hi @browniebroke @auvipy ,
my tone in previous messages might have come across differently than I intended, and I apologize if that was the case.
I’m happy to make any changes if needed, or if you’re not happy with my changes, you can close this PR.
Either way i'm happy. Thanks for your time!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@browniebroke browniebroke browniebroke left review comments

@auvipy auvipy auvipy left review comments

At least 2 approving reviews are required to merge this pull request.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

AltStyle によって変換されたページ (->オリジナル) /