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

Refs #36152 -- Suppressed duplicate warning when using "%" in alias via values().

This commit is contained in:
Jacob Walls 2025-08-26 08:54:34 -04:00
parent 183fcebf88
commit 2d453a2a68
3 changed files with 15 additions and 2 deletions

View File

@ -35,6 +35,7 @@ from django.db.models.utils import (
resolve_callables, resolve_callables,
) )
from django.utils import timezone from django.utils import timezone
from django.utils.deprecation import RemovedInDjango70Warning
from django.utils.functional import cached_property from django.utils.functional import cached_property
# The maximum number of results to fetch in a get() query. # The maximum number of results to fetch in a get() query.
@ -1394,7 +1395,12 @@ class QuerySet(AltersData):
def _values(self, *fields, **expressions): def _values(self, *fields, **expressions):
clone = self._chain() clone = self._chain()
if expressions: 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._fields = fields
clone.query.set_values(fields) clone.query.set_values(fields)
return clone return clone

View File

@ -1219,7 +1219,7 @@ class Query(BaseExpression):
if "aggregate" in {frame.function for frame in inspect.stack()}: if "aggregate" in {frame.function for frame in inspect.stack()}:
stacklevel = 5 stacklevel = 5
else: else:
# annotate() and alias(). # annotate(), alias(), and values().
stacklevel = 6 stacklevel = 6
warnings.warn( warnings.warn(
"Using percent signs in a column alias is deprecated.", "Using percent signs in a column alias is deprecated.",

View File

@ -1,5 +1,6 @@
from django.db.models import F, Sum from django.db.models import F, Sum
from django.test import TestCase, skipUnlessDBFeature from django.test import TestCase, skipUnlessDBFeature
from django.utils.deprecation import RemovedInDjango70Warning
from .models import Company, Employee, JSONFieldModel from .models import Company, Employee, JSONFieldModel
@ -34,6 +35,12 @@ class ValuesExpressionsTests(TestCase):
[{"salary": 10}, {"salary": 20}, {"salary": 30}], [{"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): def test_values_expression_alias_sql_injection(self):
crafted_alias = """injected_name" from "expressions_company"; --""" crafted_alias = """injected_name" from "expressions_company"; --"""
msg = ( msg = (