1
0
mirror of https://github.com/django/django.git synced 2025-09-10 11:09:12 +00:00

Fixed #36431 -- Returned tuples for multi-column ForeignObject in values()/values_list().

Thanks Jacob Walls and Simon Charette for tests.

Signed-off-by: SaJH <wogur981208@gmail.com>
This commit is contained in:
SaJH 2025-08-30 00:45:02 +09:00 committed by Jacob Walls
parent 2d453a2a68
commit bb7a7701b1
3 changed files with 33 additions and 4 deletions

View File

@ -2269,8 +2269,21 @@ class Query(BaseExpression):
join_info.joins,
join_info.path,
)
for target in targets:
cols.append(join_info.transform_function(target, final_alias))
if len(targets) > 1:
transformed_targets = [
join_info.transform_function(target, final_alias)
for target in targets
]
cols.append(
ColPairs(
final_alias if self.alias_cols else None,
[col.target for col in transformed_targets],
[col.output_field for col in transformed_targets],
join_info.final_field,
)
)
else:
cols.append(join_info.transform_function(targets[0], final_alias))
if cols:
self.set_select(cols)
except MultiJoin:

View File

@ -10,4 +10,6 @@ Django 5.2.6 fixes a security issue with severity "high" and several bugs in
Bugfixes
========
* ...
* Fixed a bug where using ``QuerySet.values()`` or ``values_list()`` with a
``ForeignObject`` composed of multiple fields returned incorrect results
instead of tuples of the referenced fields (:ticket:`36431`).

View File

@ -3,7 +3,7 @@ from uuid import UUID
from django.test import TestCase
from .models import Post, Tenant, User
from .models import Comment, Post, Tenant, User
class CompositePKValuesTests(TestCase):
@ -210,3 +210,17 @@ class CompositePKValuesTests(TestCase):
{"pk": self.user_3.pk, "id": self.user_3.id},
),
)
def test_foreign_object_values(self):
Comment.objects.create(id=1, user=self.user_1, integer=42)
testcases = {
"all": Comment.objects.all(),
"exclude_user_email": Comment.objects.exclude(user__email__endswith="net"),
}
for name, queryset in testcases.items():
with self.subTest(name=name):
values = list(queryset.values("user", "integer"))
self.assertEqual(
values[0]["user"], (self.user_1.tenant_id, self.user_1.id)
)
self.assertEqual(values[0]["integer"], 42)