Removed deprecated legacy connection handling for `Field.get_db_*`.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@15958 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
4a6cb38722
commit
e57cc90c16
|
@ -7,7 +7,6 @@ import math
|
|||
from itertools import tee
|
||||
|
||||
from django.db import connection
|
||||
from django.db.models.fields.subclassing import LegacyConnection
|
||||
from django.db.models.query_utils import QueryWrapper
|
||||
from django.conf import settings
|
||||
from django import forms
|
||||
|
@ -46,7 +45,6 @@ class FieldDoesNotExist(Exception):
|
|||
|
||||
class Field(object):
|
||||
"""Base class for all field types"""
|
||||
__metaclass__ = LegacyConnection
|
||||
|
||||
# Designates whether empty strings fundamentally are allowed at the
|
||||
# database level.
|
||||
|
|
|
@ -6,72 +6,7 @@ Add SubfieldBase as the __metaclass__ for your Field subclass, implement
|
|||
to_python() and the other necessary methods and everything will work seamlessly.
|
||||
"""
|
||||
|
||||
from inspect import getargspec
|
||||
from warnings import warn
|
||||
|
||||
def call_with_connection(func):
|
||||
arg_names, varargs, varkwargs, defaults = getargspec(func)
|
||||
updated = ('connection' in arg_names or varkwargs)
|
||||
if not updated:
|
||||
warn("A Field class whose %s method hasn't been updated to take a "
|
||||
"`connection` argument." % func.__name__,
|
||||
DeprecationWarning, stacklevel=3)
|
||||
|
||||
def inner(*args, **kwargs):
|
||||
if 'connection' not in kwargs:
|
||||
from django.db import connection
|
||||
kwargs['connection'] = connection
|
||||
warn("%s has been called without providing a connection argument. " %
|
||||
func.__name__, DeprecationWarning,
|
||||
stacklevel=2)
|
||||
if updated:
|
||||
return func(*args, **kwargs)
|
||||
if 'connection' in kwargs:
|
||||
del kwargs['connection']
|
||||
return func(*args, **kwargs)
|
||||
return inner
|
||||
|
||||
def call_with_connection_and_prepared(func):
|
||||
arg_names, varargs, varkwargs, defaults = getargspec(func)
|
||||
updated = (
|
||||
('connection' in arg_names or varkwargs) and
|
||||
('prepared' in arg_names or varkwargs)
|
||||
)
|
||||
if not updated:
|
||||
warn("A Field class whose %s method hasn't been updated to take "
|
||||
"`connection` and `prepared` arguments." % func.__name__,
|
||||
DeprecationWarning, stacklevel=3)
|
||||
|
||||
def inner(*args, **kwargs):
|
||||
if 'connection' not in kwargs:
|
||||
from django.db import connection
|
||||
kwargs['connection'] = connection
|
||||
warn("%s has been called without providing a connection argument. " %
|
||||
func.__name__, DeprecationWarning,
|
||||
stacklevel=2)
|
||||
if updated:
|
||||
return func(*args, **kwargs)
|
||||
if 'connection' in kwargs:
|
||||
del kwargs['connection']
|
||||
if 'prepared' in kwargs:
|
||||
del kwargs['prepared']
|
||||
return func(*args, **kwargs)
|
||||
return inner
|
||||
|
||||
class LegacyConnection(type):
|
||||
"""
|
||||
A metaclass to normalize arguments give to the get_db_prep_* and db_type
|
||||
methods on fields.
|
||||
"""
|
||||
def __new__(cls, name, bases, attrs):
|
||||
new_cls = super(LegacyConnection, cls).__new__(cls, name, bases, attrs)
|
||||
for attr in ('db_type', 'get_db_prep_save'):
|
||||
setattr(new_cls, attr, call_with_connection(getattr(new_cls, attr)))
|
||||
for attr in ('get_db_prep_lookup', 'get_db_prep_value'):
|
||||
setattr(new_cls, attr, call_with_connection_and_prepared(getattr(new_cls, attr)))
|
||||
return new_cls
|
||||
|
||||
class SubfieldBase(LegacyConnection):
|
||||
class SubfieldBase(type):
|
||||
"""
|
||||
A metaclass for custom Field subclasses. This ensures the model's attribute
|
||||
has the descriptor protocol attached to it.
|
||||
|
|
|
@ -40,7 +40,7 @@ class SmallField(models.Field):
|
|||
return value
|
||||
return Small(value[0], value[1])
|
||||
|
||||
def get_db_prep_save(self, value):
|
||||
def get_db_prep_save(self, value, connection):
|
||||
return unicode(value)
|
||||
|
||||
def get_prep_lookup(self, lookup_type, value):
|
||||
|
@ -70,7 +70,7 @@ class JSONField(models.TextField):
|
|||
value = json.loads(value)
|
||||
return value
|
||||
|
||||
def get_db_prep_save(self, value):
|
||||
def get_db_prep_save(self, value, connection):
|
||||
if value is None:
|
||||
return None
|
||||
return json.dumps(value)
|
||||
|
|
Loading…
Reference in New Issue