8000 Fixed #24656 -- Added missing imports to query expressions doc. · ddriddle/django@3768236 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3768236

Browse files
niconoetimgraham
authored andcommitted
Fixed #24656 -- Added missing imports to query expressions doc.
1 parent ad31bc0 commit 3768236

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,7 @@ answer newbie questions, and generally made Django that much better:
522522
Niclas Olofsson <n@niclasolofsson.se>
523523
Nicola Larosa <nico@teknico.net>
524524
Nicolas Lara <nicolaslara@gmail.com>
525+
Nicolas Noé <nicolas@niconoe.eu>
525526
Niran Babalola <niran@niran.org>
526527
Nis Jørgensen <nis@superlativ.dk>
527528
Nowell Strite <http://nowell.strite.org/>

docs/ref/models/expressions.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ Some examples
2626

2727
.. code-block:: python
2828

29+
from django.db.models import F, Count
30+
from django.db.models.functions import Length
31+
2932
# Find companies that have more employees than chairs.
3033
Company.objects.filter(num_employees__gt=F('num_chairs'))
3134

@@ -62,6 +65,12 @@ Some examples
6265
Built-in Expressions
6366
====================
6467

68+
.. note::
69+
70+
These expressions are defined in ``django.db.models.expressions`` and
71+
``django.db.models.aggregates``, but for convenience they're available and
72+
usually imported from :mod:`django.db.models`.
73+
6574
``F()`` expressions
6675
-------------------
6776

@@ -88,6 +97,7 @@ into memory and manipulated it using familiar Python operators, and then saved
8897
the object back to the database. But instead we could also have done::
8998

9099
from django.db.models import F
100+
91101
reporter = Reporters.objects.get(name='Tintin')
92102
reporter.stories_filed = F('stories_filed') + 1
93103
reporter.save()
@@ -194,6 +204,8 @@ directly support ``output_field`` you will need to wrap the expression with
194204
database functions like ``COALESCE`` and ``LOWER``, or aggregates like ``SUM``.
195205
They can be used directly::
196206

207+
from django.db.models import Func, F
208+
197209
queryset.annotate(field_lower=Func(F('field'), function='LOWER'))
198210

199211
or they can be used to build a library of database functions::
@@ -259,6 +271,8 @@ like ``Sum()`` and ``Count()``, inherit from ``Aggregate()``.
259271
Since ``Aggregate``\s are expressions and wrap expressions, you can represent
260272
some complex computations::
261273

274+
from django.db.models import Count
275+
262276
Company.objects.annotate(
263277
managers_required=(Count('num_employees') / 4) + Count('num_managers'))
264278

@@ -314,6 +328,8 @@ Creating your own aggregate is extremely easy. At a minimum, you need
314328
to define ``function``, but you can also completely customize the
315329
SQL that is generated. Here's a brief example::
316330

331+
from django.db.models import Aggregate
332+
317333
class Count(Aggregate):
318334
# supports COUNT(distinct field)
319335
function = 'COUNT'
@@ -578,6 +594,7 @@ to play nice with other query expressions::
578594

579595
Let's see how it works::
580596

597+
>>> from django.db.models import F, Value, CharField
581598
>>> qs = Company.objects.annotate(
582599
... tagline=Coalesce([
583600
... F('motto'),

0 commit comments

Comments
 (0)
0