@@ -24,11 +24,13 @@ In this guide, you can learn how to use {+django-odm+} to specify
24
24
a database query.
25
25
26
26
The Django ``QuerySet`` API provides functions that allow you to retrieve
27
- model objects. To run a MongoDB database query, create a ``QuerySet`` object
28
- that specifies your query and the model you're querying. Then, {+django-odm+}
29
- runs the operation on the MongoDB collection that the model represents.
27
+ model objects. To run a MongoDB database query, call ``QuerySet`` functions
28
+ on your model's ``Manager``. The ``Manager`` class handles database
29
+ operations and allows you to interact with your MongoDB data by referencing
30
+ Django models. By default, Django adds a ``Manager`` named ``objects``
31
+ to every model class.
30
32
31
- When you assign a ``QuerySet`` object to a variable, {+django-odm+} does not
33
+ When you assign a ``QuerySet`` to a variable, {+django-odm+} does not
32
34
perform the operation until you evaluate the variable, usually by printing. After
33
35
evaluating the ``QuerySet``, Django saves the query results in the ``QuerySet``
34
36
cache. Future evaluations of the ``QuerySet`` reuse the cached results.
@@ -83,9 +85,9 @@ visit the :ref:`django-get-started` tutorial.
83
85
Run a Query
84
86
-----------
85
87
86
- To query your MongoDB data, call a ``QuerySet`` function on the
87
- model that represents your collection and specify your matching
88
- criteria in a query filter.
88
+ To query your MongoDB data, call a ``QuerySet`` function on your
89
+ model's ``Manager`` class and specify your matching criteria in a
90
+ query filter.
89
91
90
92
This section describes how to use the following functions from
91
93
the ``QuerySet`` API:
@@ -101,11 +103,10 @@ Retrieve All Documents
101
103
~~~~~~~~~~~~~~~~~~~~~~
102
104
103
105
To retrieve all documents from a collection, call the ``all()``
104
- function on the collection 's corresponding Django model .
106
+ function on your model 's ``Manager`` class .
105
107
106
- The following example calls the ``all()`` function on the
107
- ``Movie`` model to retrieve all documents in the ``sample_mflix.movies``
108
- collection:
108
+ The following example calls the ``all()`` function to retrieve
109
+ all documents in the ``sample_mflix.movies`` collection:
109
110
110
111
.. io-code-block::
111
112
:copyable:
@@ -135,12 +136,12 @@ Retrieve Matching Documents
135
136
~~~~~~~~~~~~~~~~~~~~~~~~~~~
136
137
137
138
To query a collection for documents that match a set of criteria,
138
- call the ``filter()`` function on the collection 's corresponding Django
139
- model. Pass a query filter to the ``filter()`` function that specifies your
139
+ call the ``filter()`` function on your model 's ``Manager`` class.
140
+ Pass a query filter to the ``filter()`` function that specifies your
140
141
query criteria.
141
142
142
- The following example calls the ``filter()`` function on the
143
- ``Movie`` model to query the ``sample_mflix.movies`` collection
143
+ The following example calls the ``filter()`` function
144
+ to query the ``sample_mflix.movies`` collection
144
145
for documents that have a ``runtime`` value of ``300``:
145
146
146
147
.. io-code-block::
@@ -163,7 +164,7 @@ Retrieve One Document
163
164
~~~~~~~~~~~~~~~~~~~~~
164
165
165
166
To retrieve one document from a collection, call the ``get()``
166
- function on the collection 's corresponding Django model . Pass
167
+ function on your model 's ``Manager`` class . Pass
167
168
a query filter to the ``get()`` function that specifies your
168
169
query criteria.
169
170
@@ -175,8 +176,8 @@ query criteria.
175
176
function.
176
177
177
178
178
- The following example calls the ``get()`` function on the
179
- ``Movie`` model to retrieve one document that has a ``title``
179
+ The following example calls the ``get()`` function
180
+ to retrieve one document that has a ``title``
180
181
value of ``"Finding Nemo"``:
181
182
182
183
.. io-code-block::
@@ -198,13 +199,12 @@ Exclude Matching Documents
198
199
~~~~~~~~~~~~~~~~~~~~~~~~~~
199
200
200
201
To query a collection for documents that do not meet your
201
- search criteria, call the ``exclude()`` function on the collection 's
202
- corresponding Django model . Pass the exclusion criteria as an
202
+ search criteria, call the ``exclude()`` function on your model 's
203
+ ``Manager`` class . Pass the exclusion criteria as an
203
204
argument to the ``exclude()`` function.
204
205
205
- The following example calls the ``exclude()`` function on the
206
- ``Movie`` model to exclude documents released before January 1,
207
- 1980 from the results:
206
+ The following example calls the ``exclude()`` function to
207
+ exclude documents released before January 1, 1980 from the results:
208
208
209
209
.. io-code-block::
210
210
:copyable:
@@ -498,7 +498,7 @@ Example
498
498
499
499
This example performs the following actions:
500
500
501
- - Calls the ``filter()`` function on the ``Movie`` model to query
501
+ - Calls the ``filter()`` function on the ``Movie`` model's ``Manager`` to query
502
502
the ``sample_mflix.movies`` collection
503
503
- Queries documents that have a ``title`` value starting
504
504
with the text ``"Rocky"``
@@ -550,7 +550,7 @@ Example
550
550
551
551
This example performs the following actions:
552
552
553
- - Calls the ``filter()`` function on the ``Movie`` model to query
553
+ - Calls the ``filter()`` function on the ``Movie`` model's ``Manager`` to query
554
554
the ``sample_mflix.movies`` collection
555
555
- Queries documents that have a ``released`` value of
556
556
``datetime(2010, 7, 16)`` (July 16, 2010)
@@ -579,10 +579,9 @@ multiple results, chain the ``first()`` function to the ``filter()``
579
579
function. Pass a query filter to the ``filter()`` function that specifies your
580
580
query criteria.
581
581
582
- The following example calls the ``filter()`` and ``first()`` functions on the
583
- ``Movie`` model to query the ``sample_mflix.movies`` collection
584
- for the first document in which the ``genres`` value is
585
- ``["Crime", "Comedy"]``:
582
+ The following example calls the ``filter()`` and ``first()`` functions
583
+ to query the ``sample_mflix.movies`` collection for the first
584
+ document in which the ``genres`` value is ``["Crime", "Comedy"]``:
586
585
587
586
.. io-code-block::
588
587
:copyable:
0 commit comments