Normalize to strings to handle form encoded data

This commit is contained in:
Trenton H
2026-04-21 10:31:48 -07:00
parent e6ffb003a6
commit 94ba9256e7
+6 -3
View File
@@ -714,15 +714,18 @@ class BatchedManyRelatedField(serializers.ManyRelatedField):
pks.append(item)
try:
found = {obj.pk: obj for obj in child.get_queryset().filter(pk__in=pks)}
# str() normalizes int vs string PKs — form data sends strings, JSON sends ints
found = {
str(obj.pk): obj for obj in child.get_queryset().filter(pk__in=pks)
}
except (TypeError, ValueError):
child.fail("incorrect_type", data_type=type(pks[0]).__name__)
result = []
for pk in pks:
if pk not in found:
if str(pk) not in found:
child.fail("does_not_exist", pk_value=pk)
result.append(found[pk])
result.append(found[str(pk)])
return result