summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJacob Walls <jacobtylerwalls@gmail.com>2025-08-26 08:54:34 -0400
committerJacob Walls <jacobtylerwalls@gmail.com>2025-08-29 13:45:08 -0400
commit2d453a2a683d73c64dc32286685eb40cbca7c425 (patch)
treec23c1006c44cd9010d06cf10e134fb0e5190cddd
parent183fcebf88aa0762a2e28477f9b24c34341a75f4 (diff)
Refs #36152 -- Suppressed duplicate warning when using "%" in alias via values().
-rw-r--r--django/db/models/query.py8
-rw-r--r--django/db/models/sql/query.py2
-rw-r--r--tests/expressions/test_queryset_values.py7
3 files changed, 15 insertions, 2 deletions
diff --git a/django/db/models/query.py b/django/db/models/query.py
index 7ae9f53bfd..d2f31d15a0 100644
--- a/django/db/models/query.py
+++ b/django/db/models/query.py
@@ -35,6 +35,7 @@ from django.db.models.utils import (
resolve_callables,
)
from django.utils import timezone
+from django.utils.deprecation import RemovedInDjango70Warning
from django.utils.functional import cached_property
# The maximum number of results to fetch in a get() query.
@@ -1394,7 +1395,12 @@ class QuerySet(AltersData):
def _values(self, *fields, **expressions):
clone = self._chain()
if expressions:
- clone = clone.annotate(**expressions)
+ # RemovedInDjango70Warning: When the deprecation ends, deindent as:
+ # clone = clone.annotate(**expressions)
+ with warnings.catch_warnings(
+ action="ignore", category=RemovedInDjango70Warning
+ ):
+ clone = clone.annotate(**expressions)
clone._fields = fields
clone.query.set_values(fields)
return clone
diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py
index df02070fa4..59d4b8f4da 100644
--- a/django/db/models/sql/query.py
+++ b/django/db/models/sql/query.py
@@ -1219,7 +1219,7 @@ class Query(BaseExpression):
if "aggregate" in {frame.function for frame in inspect.stack()}:
stacklevel = 5
else:
- # annotate() and alias().
+ # annotate(), alias(), and values().
stacklevel = 6
warnings.warn(
"Using percent signs in a column alias is deprecated.",
diff --git a/tests/expressions/test_queryset_values.py b/tests/expressions/test_queryset_values.py
index 47bd1358de..70e9166655 100644
--- a/tests/expressions/test_queryset_values.py
+++ b/tests/expressions/test_queryset_values.py
@@ -1,5 +1,6 @@
from django.db.models import F, Sum
from django.test import TestCase, skipUnlessDBFeature
+from django.utils.deprecation import RemovedInDjango70Warning
from .models import Company, Employee, JSONFieldModel
@@ -34,6 +35,12 @@ class ValuesExpressionsTests(TestCase):
[{"salary": 10}, {"salary": 20}, {"salary": 30}],
)
+ def test_values_expression_containing_percent_sign_deprecation_warns_once(self):
+ msg = "Using percent signs in a column alias is deprecated."
+ with self.assertWarnsMessage(RemovedInDjango70Warning, msg) as cm:
+ Company.objects.values(**{"alias%": F("id")})
+ self.assertEqual(len(cm.warnings), 1)
+
def test_values_expression_alias_sql_injection(self):
crafted_alias = """injected_name" from "expressions_company"; --"""
msg = (