Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion django/db/models/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
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.
Expand Down Expand Up @@ -1394,7 +1395,12 @@ def raw(self, raw_query, params=(), translations=None, using=None):
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
Expand Down
2 changes: 1 addition & 1 deletion django/db/models/sql/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -1219,7 +1219,7 @@ def check_alias(self, alias):
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.",
Expand Down
7 changes: 7 additions & 0 deletions tests/expressions/test_queryset_values.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -34,6 +35,12 @@ def test_values_expression(self):
[{"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 = (
Expand Down
Loading