diff options
| author | Josh Smeaton <josh.smeaton@gmail.com> | 2015-03-19 14:07:53 +1100 |
|---|---|---|
| committer | Josh Smeaton <josh.smeaton@gmail.com> | 2015-03-22 17:36:55 +1100 |
| commit | 02a2943e4cf95dea0294e8695cfe981f34ae6180 (patch) | |
| tree | da87f399010a9c58b013a869af0ba5ba4e14879e /docs/ref | |
| parent | 127b3873d03704f77428b984de022664b268314e (diff) | |
Fixed #24485 -- Allowed combined expressions to set output_field
Diffstat (limited to 'docs/ref')
| -rw-r--r-- | docs/ref/models/expressions.txt | 45 |
1 files changed, 34 insertions, 11 deletions
diff --git a/docs/ref/models/expressions.txt b/docs/ref/models/expressions.txt index dfc852c048..b3d2e8b8c0 100644 --- a/docs/ref/models/expressions.txt +++ b/docs/ref/models/expressions.txt @@ -161,6 +161,27 @@ values, rather than on Python values. This is documented in :ref:`using F() expressions in queries <using-f-expressions-in-filters>`. +.. _using-f-with-annotations: + +Using ``F()`` with annotations +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +``F()`` can be used to create dynamic fields on your models by combining +different fields with arithmetic:: + + company = Company.objects.annotate( + chairs_needed=F('num_employees') - F('num_chairs')) + +If the fields that you're combining are of different types you'll need +to tell Django what kind of field will be returned. Since ``F()`` does not +directly support ``output_field`` you will need to wrap the expression with +:class:`ExpressionWrapper`:: + + from django.db.models import DateTimeField, ExpressionWrapper, F + + Ticket.objects.annotate( + expires=ExpressionWrapper( + F('active_at') + F('duration'), output_field=DateTimeField())) .. _func-expressions: @@ -274,17 +295,6 @@ should define the desired ``output_field``. For example, adding an ``IntegerField()`` and a ``FloatField()`` together should probably have ``output_field=FloatField()`` defined. -.. note:: - - When you need to define the ``output_field`` for ``F`` expression - arithmetic between different types, it's necessary to surround the - expression in another expression:: - - from django.db.models import DateTimeField, Expression, F - - Race.objects.annotate(finish=Expression( - F('start') + F('duration'), output_field=DateTimeField())) - .. versionchanged:: 1.8 ``output_field`` is a new parameter. @@ -343,6 +353,19 @@ instantiating the model field as any arguments relating to data validation (``max_length``, ``max_digits``, etc.) will not be enforced on the expression's output value. +``ExpressionWrapper()`` expressions +----------------------------------- + +.. class:: ExpressionWrapper(expression, output_field) + +.. versionadded:: 1.8 + +``ExpressionWrapper`` simply surrounds another expression and provides access +to properties, such as ``output_field``, that may not be available on other +expressions. ``ExpressionWrapper`` is necessary when using arithmetic on +``F()`` expressions with different types as described in +:ref:`using-f-with-annotations`. + Conditional expressions ----------------------- |
