Skip to content

Commit cde657c

Browse files
committed
RR feedback
1 parent a1f6613 commit cde657c

File tree

2 files changed

+76
-63
lines changed

2 files changed

+76
-63
lines changed

source/includes/interact-data/specify-a-query.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,6 @@ def __str__(self):
5151
Movie.objects.filter(runtime__lte=50)
5252
# end-filter-lte
5353

54-
# start-filter-relationships
55-
Movie.objects.filter(awards__wins=93)
56-
# end-filter-relationships
57-
5854
# start-filter-combine
5955
Movie.objects.filter(
6056
(Q(title__startswith="Funny") | Q(title__startswith="Laugh"))
@@ -73,6 +69,10 @@ def __str__(self):
7369
Movie.objects.filter(genres=["Crime", "Comedy"]).first()
7470
# end-first
7571

72+
# start-filter-relationships
73+
Movie.objects.filter(awards__wins__gt=150)
74+
# end-filter-relationships
75+
7676
# start-array
7777
Movie.objects.filter(genres__overlap=["Adventure", "Family"])
7878
# end-array

source/interact-data/specify-a-query.txt

Lines changed: 72 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -23,29 +23,32 @@ Overview
2323
In this guide, you can learn how to use {+django-odm+} to specify
2424
a database query.
2525

26+
You can refine the set of documents that a query returns by creating a
27+
**query filter**. A query filter is an expression that specifies the search
28+
criteria MongoDB uses to match documents in a read or write operation.
29+
In a query filter, you can prompt {+django-odm+} to search for documents with an
30+
exact match to your query, or you can compose query filters to express more
31+
complex matching criteria.
32+
33+
Query API
34+
~~~~~~~~~
35+
2636
The Django ``QuerySet`` API provides methods that allow you to retrieve
2737
model objects. To run a MongoDB database query, call ``QuerySet`` methods
28-
on your model's ``Manager``. The ``Manager`` class handles database
38+
on your model's manager. The ``Manager`` class handles database
2939
operations and allows you to interact with your MongoDB data by referencing
3040
Django models. By default, Django adds a ``Manager`` named ``objects``
3141
to every model class.
3242

3343
When you assign a ``QuerySet`` to a variable, {+django-odm+} does not
3444
perform the operation until you evaluate the variable. After
3545
evaluating the ``QuerySet``, Django saves the query results in the ``QuerySet``
36-
cache. Future evaluations of the ``QuerySet`` reuse the cached results.
46+
cache. Future evaluations of the ``QuerySet`` use the cached results.
3747

3848
.. tip::
3949

40-
To learn more about the Django ``QuerySet`` API, see `QuerySet API reference
41-
<{+django-docs+}/ref/models/querysets/>`__ in the Django documentation.
42-
43-
You can refine the set of documents that a query returns by creating a
44-
**query filter**. A query filter is an expression that specifies the search
45-
criteria MongoDB uses to match documents in a read or write operation.
46-
In a query filter, you can prompt {+django-odm+} to search for documents with an
47-
exact match to your query, or you can compose query filters to express more
48-
complex matching criteria.
50+
To learn more about the Django ``QuerySet`` API, see `QuerySet API reference
51+
<{+django-docs+}/ref/models/querysets/>`__ in the Django documentation.
4952

5053
Sample Data
5154
~~~~~~~~~~~
@@ -86,7 +89,7 @@ Run a Query
8689
-----------
8790

8891
To query your MongoDB data, call a ``QuerySet`` method on your
89-
model's ``Manager`` class and specify your matching criteria in a
92+
model's manager and specify your matching criteria in a
9093
query filter.
9194

9295
This section describes how to use the following methods from
@@ -104,7 +107,7 @@ Retrieve All Documents
104107
~~~~~~~~~~~~~~~~~~~~~~
105108

106109
To retrieve all documents from a collection, call the ``all()``
107-
method on your model's ``Manager`` class.
110+
method on your model's manager.
108111

109112
The following example calls the ``all()`` method to retrieve
110113
all documents in the ``sample_mflix.movies`` collection:
@@ -137,7 +140,7 @@ Retrieve Matching Documents
137140
~~~~~~~~~~~~~~~~~~~~~~~~~~~
138141

139142
To query a collection for documents that match a set of criteria,
140-
call the ``filter()`` method on your model's ``Manager`` class.
143+
call the ``filter()`` method on your model's manager.
141144
Pass a query filter to the ``filter()`` method that specifies your
142145
query criteria.
143146

@@ -165,9 +168,8 @@ Retrieve One Document
165168
~~~~~~~~~~~~~~~~~~~~~
166169

167170
To retrieve one document from a collection, call the ``get()``
168-
method on your model's ``Manager`` class. Pass
169-
a query filter to the ``get()`` method that specifies your
170-
query criteria.
171+
method on your model's manager. Pass a query filter to
172+
the ``get()`` method that specifies your query criteria.
171173

172174
.. important::
173175

@@ -176,7 +178,6 @@ query criteria.
176178
that might match multiple, use the :ref:`first() <django-query-first>`
177179
method.
178180

179-
180181
The following example calls the ``get()`` method
181182
to retrieve one document that has a ``title``
182183
value of ``"Finding Nemo"``:
@@ -242,13 +243,15 @@ Run Raw Database Queries
242243
~~~~~~~~~~~~~~~~~~~~~~~~
243244

244245
If you want to run complex queries that Django's query API
245-
cannot express, you can use the ``raw_aggregate()`` method. This
246+
does not support, you can use the ``raw_aggregate()`` method. This
246247
method allows you to specify your query criteria in a MongoDB
247248
aggregation pipeline, which you pass as an argument to
248249
``raw_aggregate()``.
249250

250-
To learn how to run raw database queries, see the :ref:`django-raw-queries`
251-
guide.
251+
.. TODO: To learn how to run raw database queries, see the :ref:`django-raw-queries`
252+
guide.
253+
254+
.. _django-query-filter:
252255

253256
Customize Your Query Filter
254257
---------------------------
@@ -271,7 +274,7 @@ expression matching, and year value matching for datetime fields.
271274

272275
To view a full list of lookup types, see `Field lookups
273276
<{+django-docs+}/ref/models/querysets/#field-lookups>`__ in the
274-
``QuerySet`` Django documentation.
277+
``QuerySet`` Django API reference.
275278

276279
This section describes how to refine your query filters
277280
in the following ways:
@@ -296,33 +299,33 @@ matching criteria for text values:
296299
* - Lookup Type
297300
- Description
298301

299-
* - ``__exact``
302+
* - ``exact``
300303
- | Specifies an exact text match. If you do not provide a
301304
lookup type in your query filter, {+django-odm+} uses this
302305
type by default.
303306

304307
| You can specify a case-insensitive exact text match
305-
by using ``__iexact``.
306-
* - ``__contains``
308+
by using ``iexact``.
309+
* - ``contains``
307310
- | Specifies a partial text match.
308311

309312
| You can specify a case-insensitive partial text match
310-
by using ``__icontains``.
311-
* - ``__startswith``
313+
by using ``icontains``.
314+
* - ``startswith``
312315
- | Matches field values that begin with the specified text.
313316

314317
| You can specify a case-insensitive partial text match
315-
by using ``__istartswith``.
316-
* - ``__endswith``
318+
by using ``istartswith``.
319+
* - ``endswith``
317320
- | Matches field values that end with the specified text.
318321

319322
| You can specify a case-insensitive partial text match
320-
by using ``__iendswith``.
323+
by using ``iendswith``.
321324

322325
Example
323326
```````
324327

325-
The following example uses the ``__contains`` lookup to
328+
The following example uses the ``contains`` lookup to
326329
query for documents in which the ``plot`` value
327330
includes the text ``"coming-of-age"``:
328331

@@ -363,27 +366,27 @@ against a specified value:
363366
* - Lookup Type
364367
- Description
365368

366-
* - ``__gt``
369+
* - ``gt``
367370
- | Matches field values that are greater than the
368371
specified value.
369372

370-
* - ``__gte``
373+
* - ``gte``
371374
- | Matches field values that are greater than or equal
372375
to the specified value.
373376

374-
* - ``__lt``
377+
* - ``lt``
375378
- | Matches field values that are less than the specified
376379
value.
377380

378-
* - ``__lte``
381+
* - ``lte``
379382
- | Matches field values that are less or equal to than the specified
380383
value.
381384

382385

383386
Example
384387
```````
385388

386-
The following example uses the ``__lte`` lookup to
389+
The following example uses the ``lte`` lookup to
387390
query for documents in which the ``runtime`` value
388391
is less than or equal to ``50``:
389392

@@ -488,12 +491,15 @@ You can provide a sort order for your query results by using the
488491
of a given field, pass the field name as an argument. To specify a descending
489492
sort, pass the field name prefixed with a minus symbol (``-``) as an argument.
490493

494+
You can pass multiple field names, separated by commas, to the ``order_by()`` method.
495+
{+django-odm+} sorts results by each field in the order provided.
496+
491497
Example
492498
```````
493499

494500
This example performs the following actions:
495501

496-
- Calls the ``filter()`` method on the ``Movie`` model's ``Manager`` to query
502+
- Calls the ``filter()`` method on the ``Movie`` model's manager to query
497503
the ``sample_mflix.movies`` collection
498504
- Queries documents that have a ``title`` value starting
499505
with the text ``"Rocky"``
@@ -517,8 +523,8 @@ This example performs the following actions:
517523

518524
.. _django-query-limit:
519525

520-
Limit Results
521-
~~~~~~~~~~~~~
526+
Skip and Limit Results
527+
~~~~~~~~~~~~~~~~~~~~~~
522528

523529
You can specify the number of results that a query returns
524530
by using Python's array-slicing syntax, as shown
@@ -531,7 +537,7 @@ in the following code:
531537

532538
Replace the ``<start>`` and ``<end>`` placeholders with integer
533539
values representing the subset of results you want to return.
534-
The start value is non-inclusive and the end value is inclusive.
540+
The start value is exclusive and the end value is inclusive.
535541

536542
If you omit the ``<start>`` value, the query returns each result,
537543
beginning with the first match, until it returns the number specified
@@ -618,16 +624,19 @@ field you want to query, as shown in the following code:
618624

619625
Model.objects.filter(<model field>__<embedded model field>=<value>)
620626

627+
You can also use :ref:`field lookups <django-query-filter>` to refine
628+
your embedded model query. Add your lookup type after the embedded
629+
model field you're querying, prefixed by double underscores.
630+
621631
Example
622632
```````
623633

624-
This example uses a lookup that spans the ``Movie`` and ``Award``
625-
relationship. The ``Movie`` model's ``awards`` field has an
626-
embedded ``Award`` model value. This embedded model represents
627-
the ``awards`` object field in the ``sample_mflix.movies`` collection
634+
This example uses a lookup to query the ``Award`` model, which is embedded
635+
in the ``Movie`` model. This embedded model represents the
636+
``awards`` field in the ``sample_mflix.movies`` collection
628637
and its nested ``wins``, ``nominations``, and ``text`` fields. The
629-
following code uses a lookup to query for documents in which the ``awards.wins``
630-
nested field value is ``93``:
638+
following code queries for documents in which the ``awards.wins``
639+
nested field value is greater than ``150``:
631640

632641
.. io-code-block::
633642
:copyable:
@@ -640,17 +649,21 @@ nested field value is ``93``:
640649
.. output::
641650
:visible: false
642651

643-
<QuerySet [<Movie: The Queen>, <Movie: Dallas Buyers Club>]>
652+
<QuerySet [<Movie: The Lord of the Rings: The Return of the King>,
653+
<Movie: No Country for Old Men>, <Movie: Slumdog Millionaire>,
654+
<Movie: Boyhood>, <Movie: The Social Network>, <Movie: Inception>,
655+
<Movie: Gravity>, <Movie: Gravity>, <Movie: The Artist>,
656+
<Movie: 12 Years a Slave>, <Movie: Birdman: Or (The Unexpected Virtue of Ignorance)>]>
644657

645658
.. _django-query-arrayfield:
646659

647660
Query an ArrayField
648-
~~~~~~~~~~~~~~~~~
661+
~~~~~~~~~~~~~~~~~~~
649662

650663
You can query data stored in an ``ArrayField`` value
651664
by using the standard query syntax. {+django-odm+} provides
652665
additional field lookup types for querying ``ArrayField`` values,
653-
as described in the following table:
666+
which are described in the following table:
654667

655668
.. list-table::
656669
:header-rows: 1
@@ -659,27 +672,27 @@ as described in the following table:
659672
* - Lookup Type
660673
- Description
661674

662-
* - ``__contains``
675+
* - ``contains``
663676
- | Matches fields that store the provided value as
664677
a subset of their data. This ``ArrayField`` lookup overrides the
665-
``__contains`` lookup described in the :ref:`django-query-text-match`
678+
``contains`` lookup described in the :ref:`django-query-text-match`
666679
section of this guide.
667680

668-
* - ``__contained_by``
681+
* - ``contained_by``
669682
- | Matches fields that store a subset of the provided values.
670-
This lookup type is the inverse of ``__contains``.
683+
This lookup type is the inverse of ``contains``.
671684

672-
* - ``__overlap``
685+
* - ``overlap``
673686
- | Matches field that store any of the provided values.
674687

675-
* - ``__len``
688+
* - ``len``
676689
- | Matches fields that store the number of values provided.
677690

678691
Example
679692
```````
680693

681-
The following example uses the ``__overlap`` lookup to query
682-
for documents whose ``genres`` field stores any values in the
694+
The following example uses the ``overlap`` lookup to query
695+
for documents whose ``genres`` field contains any values in the
683696
``["Adventure", "Family"]`` array:
684697

685698
.. io-code-block::
@@ -713,7 +726,7 @@ section. Chain each field and nested field name together, separated
713726
by double underscores (``__``), until you reach the field you want
714727
to query.
715728

716-
The following example queries the ``imdb`` field, a ``JSONField``,
729+
The following example queries the ``JSONField``-valued ``imdb`` field
717730
for values of its nested ``votes`` field that exceed ``900000``:
718731

719732
.. io-code-block::
@@ -811,7 +824,7 @@ for a document whose primary key is ``ObjectId("573a1394f29313caabce0d37")``:
811824

812825
The ``Movie`` model, which represents the ``sample_mflix.movies`` collection,
813826
uses the ``id`` field as its primary key. The following example constructs
814-
the same query as the preceding code:
827+
the same query as the preceding code by using the ``id`` field:
815828

816829
.. literalinclude:: /includes/interact-data/specify-a-query.py
817830
:language: python

0 commit comments

Comments
 (0)