From aa10f57d9460f121dea2ec2635e478ed02cc18b5 Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Tue, 17 Jun 2014 11:57:16 -0400 Subject: [PATCH] [1.7.x] Fixed #22819 -- Renamed output_type -> output_field in query expression API. Thanks jorgecarleitao for the suggestion. Backport of 95cc0e15b4 from master --- django/contrib/gis/db/models/lookups.py | 6 +++--- django/db/models/lookups.py | 24 ++++++++++++------------ django/db/models/sql/aggregates.py | 2 +- django/db/models/sql/datastructures.py | 6 +++--- django/db/models/sql/query.py | 4 ++-- docs/ref/models/custom-lookups.txt | 10 +++++----- tests/custom_lookups/tests.py | 6 +++--- 7 files changed, 29 insertions(+), 29 deletions(-) diff --git a/django/contrib/gis/db/models/lookups.py b/django/contrib/gis/db/models/lookups.py index cad6c69000f..8a6b1127267 100644 --- a/django/contrib/gis/db/models/lookups.py +++ b/django/contrib/gis/db/models/lookups.py @@ -16,13 +16,13 @@ class GISLookup(Lookup): if not geo_fld: raise ValueError('No geographic field found in expression.') self.rhs.srid = geo_fld.srid - db_type = self.lhs.output_type.db_type(connection=connection) - params = self.lhs.output_type.get_db_prep_lookup( + db_type = self.lhs.output_field.db_type(connection=connection) + params = self.lhs.output_field.get_db_prep_lookup( self.lookup_name, self.rhs, connection=connection) lhs_sql, lhs_params = self.process_lhs(qn, connection) # lhs_params not currently supported. assert not lhs_params data = (lhs_sql, db_type) 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 diff --git a/django/db/models/lookups.py b/django/db/models/lookups.py index b94090ea1a2..8242602e82f 100644 --- a/django/db/models/lookups.py +++ b/django/db/models/lookups.py @@ -26,16 +26,16 @@ class RegisterLookupMixin(object): def get_lookup(self, lookup_name): found = self._get_lookup(lookup_name) - if found is None and hasattr(self, 'output_type'): - return self.output_type.get_lookup(lookup_name) + if found is None and hasattr(self, 'output_field'): + return self.output_field.get_lookup(lookup_name) if found is not None and not issubclass(found, Lookup): return None return found def get_transform(self, lookup_name): found = self._get_lookup(lookup_name) - if found is None and hasattr(self, 'output_type'): - return self.output_type.get_transform(lookup_name) + if found is None and hasattr(self, 'output_field'): + return self.output_field.get_transform(lookup_name) if found is not None and not issubclass(found, Transform): return None return found @@ -64,8 +64,8 @@ class Transform(RegisterLookupMixin): raise NotImplementedError @cached_property - def output_type(self): - return self.lhs.output_type + def output_field(self): + return self.lhs.output_field def relabeled_clone(self, relabels): return self.__class__(self.lhs.relabeled_clone(relabels)) @@ -82,11 +82,11 @@ class Lookup(RegisterLookupMixin): self.rhs = self.get_prep_lookup() 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): 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)) def process_lhs(self, qn, connection, lhs=None): @@ -138,8 +138,8 @@ class BuiltinLookup(Lookup): def process_lhs(self, qn, connection, lhs=None): lhs_sql, params = super(BuiltinLookup, self).process_lhs( qn, connection, lhs) - field_internal_type = self.lhs.output_type.get_internal_type() - db_type = self.lhs.output_type.db_type(connection=connection) + field_internal_type = self.lhs.output_field.get_internal_type() + db_type = self.lhs.output_field.db_type(connection=connection) lhs_sql = connection.ops.field_cast_sql( db_type, field_internal_type) % lhs_sql lhs_sql = connection.ops.lookup_cast(self.lookup_name) % lhs_sql @@ -203,7 +203,7 @@ class In(BuiltinLookup): lookup_name = 'in' 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) if not params: # TODO: check why this leads to circular import @@ -299,7 +299,7 @@ class DateLookup(BuiltinLookup): def process_lhs(self, qn, connection, lhs=None): from django.db.models import DateTimeField 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 sql, tz_params = connection.ops.datetime_extract_sql(self.extract_type, lhs, tzname) return connection.ops.lookup_cast(self.lookup_name) % sql, tz_params diff --git a/django/db/models/sql/aggregates.py b/django/db/models/sql/aggregates.py index ce2968c4458..8274d436211 100644 --- a/django/db/models/sql/aggregates.py +++ b/django/db/models/sql/aggregates.py @@ -103,7 +103,7 @@ class Aggregate(RegisterLookupMixin): return [] @property - def output_type(self): + def output_field(self): return self.field diff --git a/django/db/models/sql/datastructures.py b/django/db/models/sql/datastructures.py index f47e25208c2..f9c9c259de6 100644 --- a/django/db/models/sql/datastructures.py +++ b/django/db/models/sql/datastructures.py @@ -12,7 +12,7 @@ class Col(object): return "%s.%s" % (qn(self.alias), qn(self.target.column)), [] @property - def output_type(self): + def output_field(self): return self.source def relabeled_clone(self, relabels): @@ -22,10 +22,10 @@ class Col(object): return [(self.alias, self.target.column)] def get_lookup(self, name): - return self.output_type.get_lookup(name) + return self.output_field.get_lookup(name) def get_transform(self, name): - return self.output_type.get_transform(name) + return self.output_field.get_transform(name) def prepare(self): return self diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py index 6421f38796a..c29b08556f5 100644 --- a/django/db/models/sql/query.py +++ b/django/db/models/sql/query.py @@ -1102,7 +1102,7 @@ class Query(object): raise FieldError( "Unsupported lookup '%s' for %s or join on the field not " "permitted." % - (lookup, lhs.output_type.__class__.__name__)) + (lookup, lhs.output_field.__class__.__name__)) lookups = lookups[1:] def build_filter(self, filter_expr, branch_negated=False, current_negated=False, @@ -1190,7 +1190,7 @@ class Query(object): raise FieldError( "Join on field '%s' not permitted. Did you " "misspell '%s' for the lookup type?" % - (col.output_type.name, lookups[0])) + (col.output_field.name, lookups[0])) if len(lookups) > 1: raise FieldError("Nested lookup '%s' not supported." % LOOKUP_SEP.join(lookups)) diff --git a/docs/ref/models/custom-lookups.txt b/docs/ref/models/custom-lookups.txt index 15db129e506..ef97a84e2a9 100644 --- a/docs/ref/models/custom-lookups.txt +++ b/docs/ref/models/custom-lookups.txt @@ -141,11 +141,11 @@ this case where there is no other lookup specified, Django interprets ``change__abs=27`` as ``change__abs__exact=27``. 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`` 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 -``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``. Writing an efficient abs__lt lookup @@ -315,10 +315,10 @@ to this API. field. Generally speaking, you will not need to override ``get_lookup()`` 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 - lookups. The output_type should be a field. + The ``output_field`` attribute is used by the ``get_lookup()`` method to + check for lookups. The ``output_field`` should be a field. Note that this documentation lists only the public methods of the API. diff --git a/tests/custom_lookups/tests.py b/tests/custom_lookups/tests.py index 5f9517cb63b..c89b268f6e1 100644 --- a/tests/custom_lookups/tests.py +++ b/tests/custom_lookups/tests.py @@ -46,7 +46,7 @@ class YearTransform(models.Transform): return connection.ops.date_extract_sql('year', lhs_sql), params @property - def output_type(self): + def output_field(self): return models.IntegerField() @@ -98,7 +98,7 @@ class SQLFunc(models.Lookup): return '%s()', [self.name] @property - def output_type(self): + def output_field(self): return CustomField() @@ -342,7 +342,7 @@ class TrackCallsYearTransform(YearTransform): return connection.ops.date_extract_sql('year', lhs_sql), params @property - def output_type(self): + def output_field(self): return models.IntegerField() def get_lookup(self, lookup_name):