diff --git a/AUTHORS b/AUTHORS
index ebf5c49..f6f32a0 100644
a
|
b
|
answer newbie questions, and generally made Django that much better:
|
522 | 522 | Niclas Olofsson <n@niclasolofsson.se> |
523 | 523 | Nicola Larosa <nico@teknico.net> |
524 | 524 | Nicolas Lara <nicolaslara@gmail.com> |
| 525 | Nicolas Noé <nicolas@niconoe.eu> |
525 | 526 | Niran Babalola <niran@niran.org> |
526 | 527 | Nis Jørgensen <nis@superlativ.dk> |
527 | 528 | Nowell Strite <http://nowell.strite.org/> |
diff --git a/docs/ref/models/expressions.txt b/docs/ref/models/expressions.txt
index b180327..8055744 100644
a
|
b
|
Some examples
|
26 | 26 | |
27 | 27 | .. code-block:: python |
28 | 28 | |
| 29 | from django.db.models import F, Count |
| 30 | from django.db.models.functions import Length |
| 31 | |
29 | 32 | # Find companies that have more employees than chairs. |
30 | 33 | Company.objects.filter(num_employees__gt=F('num_chairs')) |
31 | 34 | |
… |
… |
Some examples
|
62 | 65 | Built-in Expressions |
63 | 66 | ==================== |
64 | 67 | |
| 68 | .. note:: |
| 69 | |
| 70 | Technically, these expressions are defined in |
| 71 | ``django.db.models.expressions`` and ``django.db.models.aggregate``, but for |
| 72 | convenience they're imported into :mod:`django.db.models`. |
| 73 | |
65 | 74 | ``F()`` expressions |
66 | 75 | ------------------- |
67 | 76 | |
… |
… |
into memory and manipulated it using familiar Python operators, and then saved
|
88 | 97 | the object back to the database. But instead we could also have done:: |
89 | 98 | |
90 | 99 | from django.db.models import F |
| 100 | |
91 | 101 | reporter = Reporters.objects.get(name='Tintin') |
92 | 102 | reporter.stories_filed = F('stories_filed') + 1 |
93 | 103 | reporter.save() |
… |
… |
directly support ``output_field`` you will need to wrap the expression with
|
194 | 204 | database functions like ``COALESCE`` and ``LOWER``, or aggregates like ``SUM``. |
195 | 205 | They can be used directly:: |
196 | 206 | |
| 207 | from django.db.models import Func, F |
| 208 | |
197 | 209 | queryset.annotate(field_lower=Func(F('field'), function='LOWER')) |
198 | 210 | |
199 | 211 | or they can be used to build a library of database functions:: |
… |
… |
Creating your own aggregate is extremely easy. At a minimum, you need
|
314 | 326 | to define ``function``, but you can also completely customize the |
315 | 327 | SQL that is generated. Here's a brief example:: |
316 | 328 | |
| 329 | from django.db.models import Aggregate |
| 330 | |
317 | 331 | class Count(Aggregate): |
318 | 332 | # supports COUNT(distinct field) |
319 | 333 | function = 'COUNT' |
… |
… |
to play nice with other query expressions::
|
577 | 591 | self.expressions = expressions |
578 | 592 | |
579 | 593 | Let's see how it works:: |
580 | | |
| 594 | >>> from django.db.models import F, Value, CharField |
581 | 595 | >>> qs = Company.objects.annotate( |
582 | 596 | ... tagline=Coalesce([ |
583 | 597 | ... F('motto'), |