1
0
mirror of https://github.com/django/django.git synced 2025-11-07 07:15:35 +00:00

Fixed #35149 -- Fixed crashes of db_default with unresolvable output field.

Field.db_default accepts either literal Python values or compilables
(as_sql) and wrap the former ones in Value internally.

While 1e38f11 added support for automatic resolving of output fields for
types such as str, int, float, and other unambigous ones it's cannot do
so for all types such as dict or even contrib.postgres and contrib.gis
primitives.

When a literal, non-compilable, value is provided it likely make the
most sense to bind its output field to the field its attached to avoid
forcing the user to provide an explicit `Value(output_field)`.

Thanks David Sanders for the report.
This commit is contained in:
Simon Charette
2024-01-28 12:02:33 -05:00
committed by Mariusz Felisiak
parent fe1cb62f5c
commit e67d7d70fa
6 changed files with 50 additions and 20 deletions

View File

@@ -412,14 +412,13 @@ class BaseDatabaseSchemaEditor:
"""Return the sql and params for the field's database default."""
from django.db.models.expressions import Value
db_default = field._db_default_expression
sql = (
self._column_default_sql(field)
if isinstance(field.db_default, Value)
else "(%s)"
self._column_default_sql(field) if isinstance(db_default, Value) else "(%s)"
)
query = Query(model=field.model)
compiler = query.get_compiler(connection=self.connection)
default_sql, params = compiler.compile(field.db_default)
default_sql, params = compiler.compile(db_default)
if self.connection.features.requires_literal_defaults:
# Some databases doesn't support parameterized defaults (Oracle,
# SQLite). If this is the case, the individual schema backend

View File

@@ -219,12 +219,6 @@ class Field(RegisterLookupMixin):
self.remote_field = rel
self.is_relation = self.remote_field is not None
self.default = default
if db_default is not NOT_PROVIDED and not hasattr(
db_default, "resolve_expression"
):
from django.db.models.expressions import Value
db_default = Value(db_default)
self.db_default = db_default
self.editable = editable
self.serialize = serialize
@@ -408,7 +402,7 @@ class Field(RegisterLookupMixin):
continue
connection = connections[db]
if not getattr(self.db_default, "allowed_default", False) and (
if not getattr(self._db_default_expression, "allowed_default", False) and (
connection.features.supports_expression_defaults
):
msg = f"{self.db_default} cannot be used in db_default."
@@ -994,7 +988,7 @@ class Field(RegisterLookupMixin):
from django.db.models.expressions import DatabaseDefault
if isinstance(value, DatabaseDefault):
return self.db_default
return self._db_default_expression
return value
def get_prep_value(self, value):
@@ -1047,6 +1041,17 @@ class Field(RegisterLookupMixin):
return return_None
return str # return empty string
@cached_property
def _db_default_expression(self):
db_default = self.db_default
if db_default is not NOT_PROVIDED and not hasattr(
db_default, "resolve_expression"
):
from django.db.models.expressions import Value
db_default = Value(db_default, self)
return db_default
def get_choices(
self,
include_blank=True,