diff options
| author | SaJH <wogur981208@gmail.com> | 2025-08-30 00:45:02 +0900 |
|---|---|---|
| committer | Jacob Walls <jacobtylerwalls@gmail.com> | 2025-08-29 15:36:09 -0400 |
| commit | ace59cb83b87a4fdeab29424ea134e78de24fb27 (patch) | |
| tree | 48ecdd9630ca529f6d66e2ec68be545bb51eb3ee | |
| parent | 16a12a979989565f471ef1a2011bab59f8226f05 (diff) | |
[5.2.x] 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>
Backport of bb7a7701b1a0e8fffe14dcebf5d5bac7f176c02a from main
| -rw-r--r-- | django/db/models/sql/query.py | 17 | ||||
| -rw-r--r-- | docs/releases/5.2.6.txt | 4 | ||||
| -rw-r--r-- | tests/composite_pk/test_values.py | 16 |
3 files changed, 33 insertions, 4 deletions
diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py index 92a09c5840..9b44d017ff 100644 --- a/django/db/models/sql/query.py +++ b/django/db/models/sql/query.py @@ -2242,8 +2242,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: diff --git a/docs/releases/5.2.6.txt b/docs/releases/5.2.6.txt index c5d79894ff..69646c8e10 100644 --- a/docs/releases/5.2.6.txt +++ b/docs/releases/5.2.6.txt @@ -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`). diff --git a/tests/composite_pk/test_values.py b/tests/composite_pk/test_values.py index 03a9a85496..6df8d417b5 100644 --- a/tests/composite_pk/test_values.py +++ b/tests/composite_pk/test_values.py @@ -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) |
