Skip to content

Commit 2d453a2

Browse files
Refs #36152 -- Suppressed duplicate warning when using "%" in alias via values().
1 parent 183fceb commit 2d453a2

3 files changed

Lines changed: 15 additions & 2 deletions

File tree

django/db/models/query.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
resolve_callables,
3636
)
3737
from django.utils import timezone
38+
from django.utils.deprecation import RemovedInDjango70Warning
3839
from django.utils.functional import cached_property
3940

4041
# The maximum number of results to fetch in a get() query.
@@ -1394,7 +1395,12 @@ def raw(self, raw_query, params=(), translations=None, using=None):
13941395
def _values(self, *fields, **expressions):
13951396
clone = self._chain()
13961397
if expressions:
1397-
clone = clone.annotate(**expressions)
1398+
# RemovedInDjango70Warning: When the deprecation ends, deindent as:
1399+
# clone = clone.annotate(**expressions)
1400+
with warnings.catch_warnings(
1401+
action="ignore", category=RemovedInDjango70Warning
1402+
):
1403+
clone = clone.annotate(**expressions)
13981404
clone._fields = fields
13991405
clone.query.set_values(fields)
14001406
return clone

django/db/models/sql/query.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1219,7 +1219,7 @@ def check_alias(self, alias):
12191219
if "aggregate" in {frame.function for frame in inspect.stack()}:
12201220
stacklevel = 5
12211221
else:
1222-
# annotate() and alias().
1222+
# annotate(), alias(), and values().
12231223
stacklevel = 6
12241224
warnings.warn(
12251225
"Using percent signs in a column alias is deprecated.",

tests/expressions/test_queryset_values.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from django.db.models import F, Sum
22
from django.test import TestCase, skipUnlessDBFeature
3+
from django.utils.deprecation import RemovedInDjango70Warning
34

45
from .models import Company, Employee, JSONFieldModel
56

@@ -34,6 +35,12 @@ def test_values_expression(self):
3435
[{"salary": 10}, {"salary": 20}, {"salary": 30}],
3536
)
3637

38+
def test_values_expression_containing_percent_sign_deprecation_warns_once(self):
39+
msg = "Using percent signs in a column alias is deprecated."
40+
with self.assertWarnsMessage(RemovedInDjango70Warning, msg) as cm:
41+
Company.objects.values(**{"alias%": F("id")})
42+
self.assertEqual(len(cm.warnings), 1)
43+
3744
def test_values_expression_alias_sql_injection(self):
3845
crafted_alias = """injected_name" from "expressions_company"; --"""
3946
msg = (

0 commit comments

Comments
 (0)