mirror of https://github.com/django/django.git
[1.7.x] Fixed #22819 -- Renamed output_type -> output_field in query expression API.
Thanks jorgecarleitao for the suggestion.
Backport of 95cc0e15b4
from master
This commit is contained in:
parent
57a770b8e5
commit
aa10f57d94
|
@ -16,13 +16,13 @@ class GISLookup(Lookup):
|
||||||
if not geo_fld:
|
if not geo_fld:
|
||||||
raise ValueError('No geographic field found in expression.')
|
raise ValueError('No geographic field found in expression.')
|
||||||
self.rhs.srid = geo_fld.srid
|
self.rhs.srid = geo_fld.srid
|
||||||
db_type = self.lhs.output_type.db_type(connection=connection)
|
db_type = self.lhs.output_field.db_type(connection=connection)
|
||||||
params = self.lhs.output_type.get_db_prep_lookup(
|
params = self.lhs.output_field.get_db_prep_lookup(
|
||||||
self.lookup_name, self.rhs, connection=connection)
|
self.lookup_name, self.rhs, connection=connection)
|
||||||
lhs_sql, lhs_params = self.process_lhs(qn, connection)
|
lhs_sql, lhs_params = self.process_lhs(qn, connection)
|
||||||
# lhs_params not currently supported.
|
# lhs_params not currently supported.
|
||||||
assert not lhs_params
|
assert not lhs_params
|
||||||
data = (lhs_sql, db_type)
|
data = (lhs_sql, db_type)
|
||||||
spatial_sql, spatial_params = connection.ops.spatial_lookup_sql(
|
spatial_sql, spatial_params = connection.ops.spatial_lookup_sql(
|
||||||
data, self.lookup_name, self.rhs, self.lhs.output_type, qn)
|
data, self.lookup_name, self.rhs, self.lhs.output_field, qn)
|
||||||
return spatial_sql, spatial_params + params
|
return spatial_sql, spatial_params + params
|
||||||
|
|
|
@ -26,16 +26,16 @@ class RegisterLookupMixin(object):
|
||||||
|
|
||||||
def get_lookup(self, lookup_name):
|
def get_lookup(self, lookup_name):
|
||||||
found = self._get_lookup(lookup_name)
|
found = self._get_lookup(lookup_name)
|
||||||
if found is None and hasattr(self, 'output_type'):
|
if found is None and hasattr(self, 'output_field'):
|
||||||
return self.output_type.get_lookup(lookup_name)
|
return self.output_field.get_lookup(lookup_name)
|
||||||
if found is not None and not issubclass(found, Lookup):
|
if found is not None and not issubclass(found, Lookup):
|
||||||
return None
|
return None
|
||||||
return found
|
return found
|
||||||
|
|
||||||
def get_transform(self, lookup_name):
|
def get_transform(self, lookup_name):
|
||||||
found = self._get_lookup(lookup_name)
|
found = self._get_lookup(lookup_name)
|
||||||
if found is None and hasattr(self, 'output_type'):
|
if found is None and hasattr(self, 'output_field'):
|
||||||
return self.output_type.get_transform(lookup_name)
|
return self.output_field.get_transform(lookup_name)
|
||||||
if found is not None and not issubclass(found, Transform):
|
if found is not None and not issubclass(found, Transform):
|
||||||
return None
|
return None
|
||||||
return found
|
return found
|
||||||
|
@ -64,8 +64,8 @@ class Transform(RegisterLookupMixin):
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
@cached_property
|
@cached_property
|
||||||
def output_type(self):
|
def output_field(self):
|
||||||
return self.lhs.output_type
|
return self.lhs.output_field
|
||||||
|
|
||||||
def relabeled_clone(self, relabels):
|
def relabeled_clone(self, relabels):
|
||||||
return self.__class__(self.lhs.relabeled_clone(relabels))
|
return self.__class__(self.lhs.relabeled_clone(relabels))
|
||||||
|
@ -82,11 +82,11 @@ class Lookup(RegisterLookupMixin):
|
||||||
self.rhs = self.get_prep_lookup()
|
self.rhs = self.get_prep_lookup()
|
||||||
|
|
||||||
def get_prep_lookup(self):
|
def get_prep_lookup(self):
|
||||||
return self.lhs.output_type.get_prep_lookup(self.lookup_name, self.rhs)
|
return self.lhs.output_field.get_prep_lookup(self.lookup_name, self.rhs)
|
||||||
|
|
||||||
def get_db_prep_lookup(self, value, connection):
|
def get_db_prep_lookup(self, value, connection):
|
||||||
return (
|
return (
|
||||||
'%s', self.lhs.output_type.get_db_prep_lookup(
|
'%s', self.lhs.output_field.get_db_prep_lookup(
|
||||||
self.lookup_name, value, connection, prepared=True))
|
self.lookup_name, value, connection, prepared=True))
|
||||||
|
|
||||||
def process_lhs(self, qn, connection, lhs=None):
|
def process_lhs(self, qn, connection, lhs=None):
|
||||||
|
@ -138,8 +138,8 @@ class BuiltinLookup(Lookup):
|
||||||
def process_lhs(self, qn, connection, lhs=None):
|
def process_lhs(self, qn, connection, lhs=None):
|
||||||
lhs_sql, params = super(BuiltinLookup, self).process_lhs(
|
lhs_sql, params = super(BuiltinLookup, self).process_lhs(
|
||||||
qn, connection, lhs)
|
qn, connection, lhs)
|
||||||
field_internal_type = self.lhs.output_type.get_internal_type()
|
field_internal_type = self.lhs.output_field.get_internal_type()
|
||||||
db_type = self.lhs.output_type.db_type(connection=connection)
|
db_type = self.lhs.output_field.db_type(connection=connection)
|
||||||
lhs_sql = connection.ops.field_cast_sql(
|
lhs_sql = connection.ops.field_cast_sql(
|
||||||
db_type, field_internal_type) % lhs_sql
|
db_type, field_internal_type) % lhs_sql
|
||||||
lhs_sql = connection.ops.lookup_cast(self.lookup_name) % lhs_sql
|
lhs_sql = connection.ops.lookup_cast(self.lookup_name) % lhs_sql
|
||||||
|
@ -203,7 +203,7 @@ class In(BuiltinLookup):
|
||||||
lookup_name = 'in'
|
lookup_name = 'in'
|
||||||
|
|
||||||
def get_db_prep_lookup(self, value, connection):
|
def get_db_prep_lookup(self, value, connection):
|
||||||
params = self.lhs.output_type.get_db_prep_lookup(
|
params = self.lhs.output_field.get_db_prep_lookup(
|
||||||
self.lookup_name, value, connection, prepared=True)
|
self.lookup_name, value, connection, prepared=True)
|
||||||
if not params:
|
if not params:
|
||||||
# TODO: check why this leads to circular import
|
# TODO: check why this leads to circular import
|
||||||
|
@ -299,7 +299,7 @@ class DateLookup(BuiltinLookup):
|
||||||
def process_lhs(self, qn, connection, lhs=None):
|
def process_lhs(self, qn, connection, lhs=None):
|
||||||
from django.db.models import DateTimeField
|
from django.db.models import DateTimeField
|
||||||
lhs, params = super(DateLookup, self).process_lhs(qn, connection, lhs)
|
lhs, params = super(DateLookup, self).process_lhs(qn, connection, lhs)
|
||||||
if isinstance(self.lhs.output_type, DateTimeField):
|
if isinstance(self.lhs.output_field, DateTimeField):
|
||||||
tzname = timezone.get_current_timezone_name() if settings.USE_TZ else None
|
tzname = timezone.get_current_timezone_name() if settings.USE_TZ else None
|
||||||
sql, tz_params = connection.ops.datetime_extract_sql(self.extract_type, lhs, tzname)
|
sql, tz_params = connection.ops.datetime_extract_sql(self.extract_type, lhs, tzname)
|
||||||
return connection.ops.lookup_cast(self.lookup_name) % sql, tz_params
|
return connection.ops.lookup_cast(self.lookup_name) % sql, tz_params
|
||||||
|
|
|
@ -103,7 +103,7 @@ class Aggregate(RegisterLookupMixin):
|
||||||
return []
|
return []
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def output_type(self):
|
def output_field(self):
|
||||||
return self.field
|
return self.field
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -12,7 +12,7 @@ class Col(object):
|
||||||
return "%s.%s" % (qn(self.alias), qn(self.target.column)), []
|
return "%s.%s" % (qn(self.alias), qn(self.target.column)), []
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def output_type(self):
|
def output_field(self):
|
||||||
return self.source
|
return self.source
|
||||||
|
|
||||||
def relabeled_clone(self, relabels):
|
def relabeled_clone(self, relabels):
|
||||||
|
@ -22,10 +22,10 @@ class Col(object):
|
||||||
return [(self.alias, self.target.column)]
|
return [(self.alias, self.target.column)]
|
||||||
|
|
||||||
def get_lookup(self, name):
|
def get_lookup(self, name):
|
||||||
return self.output_type.get_lookup(name)
|
return self.output_field.get_lookup(name)
|
||||||
|
|
||||||
def get_transform(self, name):
|
def get_transform(self, name):
|
||||||
return self.output_type.get_transform(name)
|
return self.output_field.get_transform(name)
|
||||||
|
|
||||||
def prepare(self):
|
def prepare(self):
|
||||||
return self
|
return self
|
||||||
|
|
|
@ -1102,7 +1102,7 @@ class Query(object):
|
||||||
raise FieldError(
|
raise FieldError(
|
||||||
"Unsupported lookup '%s' for %s or join on the field not "
|
"Unsupported lookup '%s' for %s or join on the field not "
|
||||||
"permitted." %
|
"permitted." %
|
||||||
(lookup, lhs.output_type.__class__.__name__))
|
(lookup, lhs.output_field.__class__.__name__))
|
||||||
lookups = lookups[1:]
|
lookups = lookups[1:]
|
||||||
|
|
||||||
def build_filter(self, filter_expr, branch_negated=False, current_negated=False,
|
def build_filter(self, filter_expr, branch_negated=False, current_negated=False,
|
||||||
|
@ -1190,7 +1190,7 @@ class Query(object):
|
||||||
raise FieldError(
|
raise FieldError(
|
||||||
"Join on field '%s' not permitted. Did you "
|
"Join on field '%s' not permitted. Did you "
|
||||||
"misspell '%s' for the lookup type?" %
|
"misspell '%s' for the lookup type?" %
|
||||||
(col.output_type.name, lookups[0]))
|
(col.output_field.name, lookups[0]))
|
||||||
if len(lookups) > 1:
|
if len(lookups) > 1:
|
||||||
raise FieldError("Nested lookup '%s' not supported." %
|
raise FieldError("Nested lookup '%s' not supported." %
|
||||||
LOOKUP_SEP.join(lookups))
|
LOOKUP_SEP.join(lookups))
|
||||||
|
|
|
@ -141,11 +141,11 @@ this case where there is no other lookup specified, Django interprets
|
||||||
``change__abs=27`` as ``change__abs__exact=27``.
|
``change__abs=27`` as ``change__abs__exact=27``.
|
||||||
|
|
||||||
When looking for which lookups are allowable after the ``Transform`` has been
|
When looking for which lookups are allowable after the ``Transform`` has been
|
||||||
applied, Django uses the ``output_type`` attribute. We didn't need to specify
|
applied, Django uses the ``output_field`` attribute. We didn't need to specify
|
||||||
this here as it didn't change, but supposing we were applying ``AbsoluteValue``
|
this here as it didn't change, but supposing we were applying ``AbsoluteValue``
|
||||||
to some field which represents a more complex type (for example a point
|
to some field which represents a more complex type (for example a point
|
||||||
relative to an origin, or a complex number) then we may have wanted to specify
|
relative to an origin, or a complex number) then we may have wanted to specify
|
||||||
``output_type = FloatField``, which will ensure that further lookups like
|
``output_field = FloatField``, which will ensure that further lookups like
|
||||||
``abs__lte`` behave as they would for a ``FloatField``.
|
``abs__lte`` behave as they would for a ``FloatField``.
|
||||||
|
|
||||||
Writing an efficient abs__lt lookup
|
Writing an efficient abs__lt lookup
|
||||||
|
@ -315,10 +315,10 @@ to this API.
|
||||||
field. Generally speaking, you will not need to override ``get_lookup()``
|
field. Generally speaking, you will not need to override ``get_lookup()``
|
||||||
or ``get_transform()``, and can use ``register_lookup()`` instead.
|
or ``get_transform()``, and can use ``register_lookup()`` instead.
|
||||||
|
|
||||||
.. attribute:: output_type
|
.. attribute:: output_field
|
||||||
|
|
||||||
The ``output_type`` attribute is used by the ``get_lookup()`` method to check for
|
The ``output_field`` attribute is used by the ``get_lookup()`` method to
|
||||||
lookups. The output_type should be a field.
|
check for lookups. The ``output_field`` should be a field.
|
||||||
|
|
||||||
Note that this documentation lists only the public methods of the API.
|
Note that this documentation lists only the public methods of the API.
|
||||||
|
|
||||||
|
|
|
@ -46,7 +46,7 @@ class YearTransform(models.Transform):
|
||||||
return connection.ops.date_extract_sql('year', lhs_sql), params
|
return connection.ops.date_extract_sql('year', lhs_sql), params
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def output_type(self):
|
def output_field(self):
|
||||||
return models.IntegerField()
|
return models.IntegerField()
|
||||||
|
|
||||||
|
|
||||||
|
@ -98,7 +98,7 @@ class SQLFunc(models.Lookup):
|
||||||
return '%s()', [self.name]
|
return '%s()', [self.name]
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def output_type(self):
|
def output_field(self):
|
||||||
return CustomField()
|
return CustomField()
|
||||||
|
|
||||||
|
|
||||||
|
@ -342,7 +342,7 @@ class TrackCallsYearTransform(YearTransform):
|
||||||
return connection.ops.date_extract_sql('year', lhs_sql), params
|
return connection.ops.date_extract_sql('year', lhs_sql), params
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def output_type(self):
|
def output_field(self):
|
||||||
return models.IntegerField()
|
return models.IntegerField()
|
||||||
|
|
||||||
def get_lookup(self, lookup_name):
|
def get_lookup(self, lookup_name):
|
||||||
|
|
Loading…
Reference in New Issue