Ticket #23420: output_field_docs.patch

File output_field_docs.patch, 1.3 KB (added by Andy Chosak, 10 years ago)
  • docs/howto/custom-lookups.txt

    diff --git a/docs/howto/custom-lookups.txt b/docs/howto/custom-lookups.txt
    index af7c18e..1c2cde8 100644
    a b applied, Django uses the ``output_field`` attribute. We didn't need to specify  
    142142this here as it didn't change, but supposing we were applying ``AbsoluteValue``
    143143to some field which represents a more complex type (for example a point
    144144relative to an origin, or a complex number) then we may have wanted to specify
    145 ``output_field = FloatField``, which will ensure that further lookups like
    146 ``abs__lte`` behave as they would for a ``FloatField``.
     145that the transform return a ``FloatField`` type for further lookups. This can
     146be done by adding an ``output_field`` attribute to the transform::
     147
     148  from django.db.models import FloatField, Transform
     149
     150  class AbsoluteValue(Transform):
     151      lookup_name = 'abs'
     152
     153      def as_sql(self, qn, connection):
     154          lhs, params = qn.compile(self.lhs)
     155          return "ABS(%s)" % lhs, params
     156
     157      @property
     158      def output_field(self):
     159          return FloatField()
     160
     161This ensures that further lookups like ``abs__lte`` behave as they would for
     162a ``FloatField``.
    147163
    148164Writing an efficient abs__lt lookup
    149165~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Back to Top