Refs #28370 -- Removed support for the context arg of Field.from_db_value() and Expression.convert_value().

Per deprecation timeline.
This commit is contained in:
Tim Graham 2018-12-27 19:58:22 -05:00
parent 7c66aaa2b6
commit da5eb3d56c
6 changed files with 8 additions and 58 deletions

View File

@ -6,7 +6,6 @@ from django.contrib.postgres.validators import ArrayMaxLengthValidator
from django.core import checks, exceptions from django.core import checks, exceptions
from django.db.models import Field, IntegerField, Transform from django.db.models import Field, IntegerField, Transform
from django.db.models.lookups import Exact, In from django.db.models.lookups import Exact, In
from django.utils.inspect import func_supports_parameter
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from ..utils import prefix_validation_error from ..utils import prefix_validation_error
@ -112,9 +111,7 @@ class ArrayField(CheckFieldDefaultMixin, Field):
if value is None: if value is None:
return value return value
return [ return [
self.base_field.from_db_value(item, expression, connection, {}) self.base_field.from_db_value(item, expression, connection)
if func_supports_parameter(self.base_field.from_db_value, 'context') # RemovedInDjango30Warning
else self.base_field.from_db_value(item, expression, connection)
for item in value for item in value
] ]

View File

@ -7,7 +7,6 @@ from django.conf import settings
from django.core.cache.backends.base import DEFAULT_TIMEOUT, BaseCache from django.core.cache.backends.base import DEFAULT_TIMEOUT, BaseCache
from django.db import DatabaseError, connections, models, router, transaction from django.db import DatabaseError, connections, models, router, transaction
from django.utils import timezone from django.utils import timezone
from django.utils.inspect import func_supports_parameter
class Options: class Options:
@ -85,10 +84,7 @@ class DatabaseCache(BaseDatabaseCache):
converters = (connection.ops.get_db_converters(expression) + expression.get_db_converters(connection)) converters = (connection.ops.get_db_converters(expression) + expression.get_db_converters(connection))
for key, value, expires in rows: for key, value, expires in rows:
for converter in converters: for converter in converters:
if func_supports_parameter(converter, 'context'): # RemovedInDjango30Warning expires = converter(expires, expression, connection)
expires = converter(expires, expression, connection, {})
else:
expires = converter(expires, expression, connection)
if expires < timezone.now(): if expires < timezone.now():
expired_keys.append(key) expired_keys.append(key)
else: else:
@ -160,10 +156,7 @@ class DatabaseCache(BaseDatabaseCache):
expression = models.Expression(output_field=models.DateTimeField()) expression = models.Expression(output_field=models.DateTimeField())
for converter in (connection.ops.get_db_converters(expression) + for converter in (connection.ops.get_db_converters(expression) +
expression.get_db_converters(connection)): expression.get_db_converters(connection)):
if func_supports_parameter(converter, 'context'): # RemovedInDjango30Warning current_expires = converter(current_expires, expression, connection)
current_expires = converter(current_expires, expression, connection, {})
else:
current_expires = converter(current_expires, expression, connection)
exp = connection.ops.adapt_datetimefield_value(exp) exp = connection.ops.adapt_datetimefield_value(exp)
if result and mode == 'touch': if result and mode == 'touch':

View File

@ -1,5 +1,4 @@
import collections import collections
import functools
import re import re
import warnings import warnings
from itertools import chain from itertools import chain
@ -14,10 +13,7 @@ from django.db.models.sql.constants import (
from django.db.models.sql.query import Query, get_order_dir from django.db.models.sql.query import Query, get_order_dir
from django.db.transaction import TransactionManagementError from django.db.transaction import TransactionManagementError
from django.db.utils import DatabaseError, NotSupportedError from django.db.utils import DatabaseError, NotSupportedError
from django.utils.deprecation import ( from django.utils.deprecation import RemovedInDjango31Warning
RemovedInDjango30Warning, RemovedInDjango31Warning,
)
from django.utils.inspect import func_supports_parameter
FORCE = object() FORCE = object()
@ -1015,20 +1011,7 @@ class SQLCompiler:
backend_converters = self.connection.ops.get_db_converters(expression) backend_converters = self.connection.ops.get_db_converters(expression)
field_converters = expression.get_db_converters(self.connection) field_converters = expression.get_db_converters(self.connection)
if backend_converters or field_converters: if backend_converters or field_converters:
convs = [] converters[i] = (backend_converters + field_converters, expression)
for conv in (backend_converters + field_converters):
if func_supports_parameter(conv, 'context'):
warnings.warn(
'Remove the context parameter from %s.%s(). Support for it '
'will be removed in Django 3.0.' % (
conv.__self__.__class__.__name__,
conv.__name__,
),
RemovedInDjango30Warning,
)
conv = functools.partial(conv, context={})
convs.append(conv)
converters[i] = (convs, expression)
return converters return converters
def apply_converters(self, rows, converters): def apply_converters(self, rows, converters):

View File

@ -244,6 +244,9 @@ to remove usage of these features.
* ``HttpRequest.xreadlines()`` is removed. * ``HttpRequest.xreadlines()`` is removed.
* Support for the ``context`` argument of ``Field.from_db_value()`` and
``Expression.convert_value()`` is removed.
See :ref:`deprecated-features-2.1` for details on these changes, including how See :ref:`deprecated-features-2.1` for details on these changes, including how
to remove usage of these features. to remove usage of these features.

View File

@ -28,12 +28,3 @@ class CashModel(models.Model):
def __str__(self): def __str__(self):
return str(self.cash) return str(self.cash)
class CashFieldDeprecated(CashField):
def from_db_value(self, value, expression, connection, context):
return super().from_db_value(value, expression, connection)
class CashModelDeprecated(models.Model):
cash = CashFieldDeprecated()

View File

@ -1,17 +0,0 @@
from django.test import TestCase
from django.utils.deprecation import RemovedInDjango30Warning
from .models import Cash, CashModelDeprecated
class FromDBValueDeprecationTests(TestCase):
def test_deprecation(self):
msg = (
'Remove the context parameter from CashFieldDeprecated.from_db_value(). '
'Support for it will be removed in Django 3.0.'
)
CashModelDeprecated.objects.create(cash='12.50')
with self.assertWarnsMessage(RemovedInDjango30Warning, msg):
instance = CashModelDeprecated.objects.get()
self.assertIsInstance(instance.cash, Cash)