Refs #25425 -- Allowed unresolved Value() instances to be compiled.

Previously unresolved Value() instances were only allowed to be
compiled if they weren't initialized with an output_field.

Given the usage of unresolved Value() instances is relatively common in
as_sql() overrides it's less controversial to add explicit support for
this previously undefined behavior now and revisit whether or not it
should be deprecated in the future.
This commit is contained in:
Simon Charette 2020-07-12 00:49:43 -04:00 committed by Mariusz Felisiak
parent d38c34119e
commit f783a99072
2 changed files with 12 additions and 0 deletions

View File

@ -676,6 +676,10 @@ class Func(SQLiteNumericMixin, Expression):
class Value(Expression): class Value(Expression):
"""Represent a wrapped value as a node within an expression.""" """Represent a wrapped value as a node within an expression."""
# Provide a default value for `for_save` in order to allow unresolved
# instances to be compiled until a decision is taken in #25425.
for_save = False
def __init__(self, value, output_field=None): def __init__(self, value, output_field=None):
""" """
Arguments: Arguments:

View File

@ -1703,6 +1703,14 @@ class ValueTests(TestCase):
with self.assertRaisesMessage(ValueError, msg): with self.assertRaisesMessage(ValueError, msg):
ExpressionList() ExpressionList()
def test_compile_unresolved(self):
# This test might need to be revisited later on if #25425 is enforced.
compiler = Time.objects.all().query.get_compiler(connection=connection)
value = Value('foo')
self.assertEqual(value.as_sql(compiler, connection), ('%s', ['foo']))
value = Value('foo', output_field=CharField())
self.assertEqual(value.as_sql(compiler, connection), ('%s', ['foo']))
class FieldTransformTests(TestCase): class FieldTransformTests(TestCase):