summaryrefslogtreecommitdiff
path: root/django/db/models/fields/__init__.py
diff options
context:
space:
mode:
Diffstat (limited to 'django/db/models/fields/__init__.py')
-rw-r--r--django/db/models/fields/__init__.py31
1 files changed, 29 insertions, 2 deletions
diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py
index feb0441bab..d98319ef00 100644
--- a/django/db/models/fields/__init__.py
+++ b/django/db/models/fields/__init__.py
@@ -30,6 +30,7 @@ from django.utils.dateparse import (
parse_duration,
parse_time,
)
+from django.utils.deprecation import RemovedInDjango70Warning, django_file_prefixes
from django.utils.duration import duration_string
from django.utils.functional import Promise, cached_property
from django.utils.ipv6 import MAX_IPV6_ADDRESS_LENGTH, clean_ipv6_address
@@ -181,6 +182,32 @@ class Field(RegisterLookupMixin):
description = property(_description)
+ def __init_subclass__(cls, **kwargs):
+ # RemovedInDjango70Warning: When the deprecation ends, remove
+ # completely.
+ # Allow for both `get_placeholder` and `get_placeholder_sql` to
+ # be declared to ease the deprecation process for third-party apps.
+ if (
+ get_placeholder := cls.__dict__.get("get_placeholder")
+ ) is not None and "get_placeholder_sql" not in cls.__dict__:
+ warnings.warn(
+ "Field.get_placeholder is deprecated in favor of get_placeholder_sql. "
+ f"Define {cls.__module__}.{cls.__qualname__}.get_placeholder_sql "
+ "to return both SQL and parameters instead.",
+ category=RemovedInDjango70Warning,
+ skip_file_prefixes=django_file_prefixes(),
+ )
+
+ def get_placeholder_sql(self, value, compiler, connection):
+ placeholder = get_placeholder(self, value, compiler, connection)
+ if hasattr(value, "as_sql"):
+ sql, params = compiler.compile(value)
+ return placeholder % sql, params
+ return placeholder, (value,)
+
+ setattr(cls, "get_placeholder_sql", get_placeholder_sql)
+ return super().__init_subclass__(**kwargs)
+
def __init__(
self,
verbose_name=None,
@@ -2735,8 +2762,8 @@ class BinaryField(Field):
def get_internal_type(self):
return "BinaryField"
- def get_placeholder(self, value, compiler, connection):
- return connection.ops.binary_placeholder_sql(value)
+ def get_placeholder_sql(self, value, compiler, connection):
+ return connection.ops.binary_placeholder_sql(value, compiler)
def get_default(self):
if self.has_default() and not callable(self.default):