Renamed Extract -> Transform
This commit is contained in:
parent
3b18d9f3a1
commit
83173b960e
|
@ -17,7 +17,7 @@ from django.db.models.fields.related import ( # NOQA
|
||||||
from django.db.models.fields.proxy import OrderWrt # NOQA
|
from django.db.models.fields.proxy import OrderWrt # NOQA
|
||||||
from django.db.models.deletion import ( # NOQA
|
from django.db.models.deletion import ( # NOQA
|
||||||
CASCADE, PROTECT, SET, SET_NULL, SET_DEFAULT, DO_NOTHING, ProtectedError)
|
CASCADE, PROTECT, SET, SET_NULL, SET_DEFAULT, DO_NOTHING, ProtectedError)
|
||||||
from django.db.models.lookups import Lookup, Extract # NOQA
|
from django.db.models.lookups import Lookup, Transform # NOQA
|
||||||
from django.db.models import signals # NOQA
|
from django.db.models import signals # NOQA
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -39,7 +39,7 @@ class RegisterLookupMixin(object):
|
||||||
del cls.class_lookups[lookup.lookup_name]
|
del cls.class_lookups[lookup.lookup_name]
|
||||||
|
|
||||||
|
|
||||||
class Extract(RegisterLookupMixin):
|
class Transform(RegisterLookupMixin):
|
||||||
def __init__(self, lhs, lookups):
|
def __init__(self, lhs, lookups):
|
||||||
self.lhs = lhs
|
self.lhs = lhs
|
||||||
self.init_lookups = lookups[:]
|
self.init_lookups = lookups[:]
|
||||||
|
|
|
@ -19,7 +19,7 @@ from django.db.models.constants import LOOKUP_SEP
|
||||||
from django.db.models.aggregates import refs_aggregate
|
from django.db.models.aggregates import refs_aggregate
|
||||||
from django.db.models.expressions import ExpressionNode
|
from django.db.models.expressions import ExpressionNode
|
||||||
from django.db.models.fields import FieldDoesNotExist
|
from django.db.models.fields import FieldDoesNotExist
|
||||||
from django.db.models.lookups import Extract
|
from django.db.models.lookups import Transform
|
||||||
from django.db.models.query_utils import Q
|
from django.db.models.query_utils import Q
|
||||||
from django.db.models.related import PathInfo
|
from django.db.models.related import PathInfo
|
||||||
from django.db.models.sql import aggregates as base_aggregates_module
|
from django.db.models.sql import aggregates as base_aggregates_module
|
||||||
|
@ -1092,7 +1092,7 @@ class Query(object):
|
||||||
if next:
|
if next:
|
||||||
if len(lookups) == 1:
|
if len(lookups) == 1:
|
||||||
# This was the last lookup, so return value lookup.
|
# This was the last lookup, so return value lookup.
|
||||||
if issubclass(next, Extract):
|
if issubclass(next, Transform):
|
||||||
lookups.append('exact')
|
lookups.append('exact')
|
||||||
lhs = next(lhs, lookups)
|
lhs = next(lhs, lookups)
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -99,9 +99,9 @@ or where it did not exceede a certain amount
|
||||||
We will start by writing a ``AbsoluteValue`` transformer. This will use the SQL
|
We will start by writing a ``AbsoluteValue`` transformer. This will use the SQL
|
||||||
function ``ABS()`` to transform the value before comparison::
|
function ``ABS()`` to transform the value before comparison::
|
||||||
|
|
||||||
from django.db.models import Extract
|
from django.db.models import Transform
|
||||||
|
|
||||||
class AbsoluteValue(Extract):
|
class AbsoluteValue(Transform):
|
||||||
lookup_name = 'abs'
|
lookup_name = 'abs'
|
||||||
|
|
||||||
def as_sql(self, qn, connection):
|
def as_sql(self, qn, connection):
|
||||||
|
@ -118,19 +118,19 @@ We can now run the queris we had before.
|
||||||
|
|
||||||
SELECT ... WHERE ABS("experiments"."change") = 27
|
SELECT ... WHERE ABS("experiments"."change") = 27
|
||||||
|
|
||||||
By using ``Extract`` instead of ``Lookup`` it means we are able to chain
|
By using ``Transform`` instead of ``Lookup`` it means we are able to chain
|
||||||
further lookups afterwards. So
|
further lookups afterwards. So
|
||||||
``Experiment.objects.filter(change__abs__lt=27)`` will generate the following
|
``Experiment.objects.filter(change__abs__lt=27)`` will generate the following
|
||||||
SQL::
|
SQL::
|
||||||
|
|
||||||
SELECT ... WHERE ABS("experiments"."change") < 27
|
SELECT ... WHERE ABS("experiments"."change") < 27
|
||||||
|
|
||||||
Subclasses of ``Extract`` usually only operate on the left-hand side of the
|
Subclasses of ``Transform`` usually only operate on the left-hand side of the
|
||||||
expression. Further lookups will work on the transformed value. Note that in
|
expression. Further lookups will work on the transformed value. Note that in
|
||||||
this case where there is no other lookup specified, Django interprets
|
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 ``Extract`` 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_type`` 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
|
||||||
|
@ -222,7 +222,7 @@ The Query Expression API
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
A lookup can assume that the lhs responds to the query expression API.
|
A lookup can assume that the lhs responds to the query expression API.
|
||||||
Currently direct field references, aggregates and ``Extract`` instances respond
|
Currently direct field references, aggregates and ``Transform`` instances respond
|
||||||
to this API.
|
to this API.
|
||||||
|
|
||||||
.. method:: as_sql(qn, connection)
|
.. method:: as_sql(qn, connection)
|
||||||
|
@ -269,7 +269,7 @@ Lookup reference
|
||||||
|
|
||||||
The ``lhs`` (left-hand side) of a lookup tells us what we are comparing the
|
The ``lhs`` (left-hand side) of a lookup tells us what we are comparing the
|
||||||
rhs to. It is an object which implements the query expression API. This is
|
rhs to. It is an object which implements the query expression API. This is
|
||||||
likely to be a field, an aggregate or a subclass of ``Extract``.
|
likely to be a field, an aggregate or a subclass of ``Transform``.
|
||||||
|
|
||||||
.. attribute:: rhs
|
.. attribute:: rhs
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue