summaryrefslogtreecommitdiff
path: root/docs
diff options
context:
space:
mode:
authorAndy Chosak <chosak@gmail.com>2014-11-03 15:34:32 +0200
committerAnssi Kääriäinen <akaariai@gmail.com>2014-11-03 15:52:27 +0200
commitc0c78f1b707f825eee974c65515a837f8cf46e66 (patch)
treeb640256b56e2ec91c0ed4e38b06238d3a4ef1dc0 /docs
parentceb1ffcc8da92a82338582bc6801de4bc8f05e32 (diff)
Clarified custom lookups output_field documentation
Diffstat (limited to 'docs')
-rw-r--r--docs/howto/custom-lookups.txt20
1 files changed, 18 insertions, 2 deletions
diff --git a/docs/howto/custom-lookups.txt b/docs/howto/custom-lookups.txt
index d3ed726ba3..da536c4070 100644
--- a/docs/howto/custom-lookups.txt
+++ b/docs/howto/custom-lookups.txt
@@ -152,8 +152,24 @@ 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_field = FloatField``, which will ensure that further lookups like
-``abs__lte`` behave as they would for a ``FloatField``.
+that the transform returns a ``FloatField`` type for further lookups. This can
+be done by adding an ``output_field`` attribute to the transform::
+
+ from django.db.models import FloatField, Transform
+
+ class AbsoluteValue(Transform):
+ lookup_name = 'abs'
+
+ def as_sql(self, qn, connection):
+ lhs, params = qn.compile(self.lhs)
+ return "ABS(%s)" % lhs, params
+
+ @property
+ def output_field(self):
+ return FloatField()
+
+This ensures that further lookups like ``abs__lte`` behave as they would for
+a ``FloatField``.
Writing an efficient abs__lt lookup
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~