@@ -67,6 +67,15 @@ root directory:
67
67
68
68
python manage.py shell
69
69
70
+ After entering the Python shell, ensure that you import the following models and
71
+ modules:
72
+
73
+ .. code-block:: python
74
+
75
+ from <your application name>.models import Movie, Award
76
+ from django.utils import timezone
77
+ from datetime import datetime
78
+
70
79
To learn how to create a Django application that uses the ``Movie``
71
80
model and the Python interactive shell to interact with MongoDB documents,
72
81
visit the :ref:`django-get-started` tutorial.
@@ -98,6 +107,27 @@ The following example calls the ``all()`` method on the
98
107
``Movie`` model to retrieve all documents in the ``sample_mflix.movies``
99
108
collection:
100
109
110
+ .. io-code-block::
111
+ :copyable:
112
+
113
+ .. input:: /includes/interact-data/specify-a-query.py
114
+ :start-after: start-all
115
+ :end-before: end-all
116
+ :language: python
117
+
118
+ .. output::
119
+
120
+ <QuerySet [<Movie: The Great Train Robbery>, <Movie: A Corner in Wheat>,
121
+ <Movie: Winsor McCay, the Famous Cartoonist of the N.Y. Herald and His Moving Comics>,
122
+ <Movie: Traffic in Souls>, <Movie: Gertie the Dinosaur>,
123
+ <Movie: In the Land of the Head Hunters>, <Movie: The Perils of Pauline>,
124
+ <Movie: The Italian>, <Movie: Regeneration>, <Movie: Civilization>,
125
+ <Movie: Where Are My Children?>, <Movie: The Poor Little Rich Girl>,
126
+ <Movie: Wild and Woolly>, <Movie: The Blue Bird>, <Movie: From Hand to Mouth>,
127
+ <Movie: High and Dizzy>, <Movie: One Week>, <Movie: The Saphead>,
128
+ <Movie: The Ace of Hearts>, <Movie: The Four Horsemen of the Apocalypse>,
129
+ '...(remaining elements truncated)...']>
130
+
101
131
.. _django-query-retrieve-matching:
102
132
103
133
Retrieve Matching Documents
@@ -112,6 +142,18 @@ The following example calls the ``filter()`` method on the
112
142
``Movie`` model to query the ``sample_mflix.movies`` collection
113
143
for documents that have a ``runtime`` value of ``300``:
114
144
145
+ .. io-code-block::
146
+ :copyable:
147
+
148
+ .. input:: /includes/interact-data/specify-a-query.py
149
+ :start-after: start-filter
150
+ :end-before: end-filter
151
+ :language: python
152
+
153
+ .. output::
154
+
155
+ <QuerySet [<Movie: Wild Palms>, <Movie: Streets of Laredo>,
156
+ <Movie: The Way We Live Now>]>
115
157
116
158
.. _django-query-retrieve-one:
117
159
@@ -135,6 +177,17 @@ The following example calls the ``get()`` method on the
135
177
``Movie`` model to retrieve one document that has a ``title``
136
178
value of ``"Finding Nemo"``:
137
179
180
+ .. io-code-block::
181
+ :copyable:
182
+
183
+ .. input:: /includes/interact-data/specify-a-query.py
184
+ :start-after: start-get
185
+ :end-before: end-get
186
+ :language: python
187
+
188
+ .. output::
189
+
190
+ <Movie: Finding Nemo>
138
191
139
192
.. _django-query-exclude:
140
193
@@ -147,12 +200,33 @@ corresponding Django model. Pass the exclusion criteria as an
147
200
argument to the ``exclude()`` method.
148
201
149
202
The following example calls the ``exclude()`` method on the
150
- ``Movie`` model to exclude documents released after January 1,
151
- 1950 from the results:
203
+ ``Movie`` model to exclude documents released before January 1,
204
+ 1980 from the results:
205
+
206
+ .. io-code-block::
207
+ :copyable:
208
+
209
+ .. input:: /includes/interact-data/specify-a-query.py
210
+ :start-after: start-exclude
211
+ :end-before: end-exclude
212
+ :language: python
213
+
214
+ .. output::
215
+
216
+ <QuerySet [<Movie: The Iron Horse>, <Movie: Le grand jeu>,
217
+ <Movie: The Devil Is a Woman>, <Movie: Children in the Wind>,
218
+ <Movie: Too Much Johnson>, <Movie: Dots>, <Movie: The Blood of Jesus>,
219
+ <Movie: The Land>, <Movie: The Brothers and Sisters of the Toda Family>,
220
+ <Movie: People on the Alps>, <Movie: Der groèe Kènig>,
221
+ <Movie: Kate & Leopold>, <Movie: Meshes of the Afternoon>,
222
+ <Movie: The Body Snatcher>, <Movie: Let There Be Light>,
223
+ <Movie: La porta del cielo>, <Movie: A Double Life>,
224
+ <Movie: One Wonderful Sunday>, <Movie: La Terra Trema>,
225
+ <Movie: Begone Dull Care>, '...(remaining elements truncated)...']>
152
226
153
227
.. tip::
154
228
155
- The preceding example uses the ``gt `` comparison operator
229
+ The preceding example uses the ``lt `` comparison operator
156
230
to query the collection. To learn more about comparison
157
231
operators, see the :ref:`django-query-comparison` section
158
232
in this guide.
@@ -230,9 +304,28 @@ Example
230
304
```````
231
305
232
306
The following example uses the ``__contains`` lookup to
233
- query for documents in which the ``genres`` array field
234
- includes ``"Romance"``:
307
+ query for documents in which the ``plot`` field
308
+ includes ``"coming-of-age"``:
309
+
310
+ .. io-code-block::
311
+ :copyable:
312
+
313
+ .. input:: /includes/interact-data/specify-a-query.py
314
+ :start-after: start-filter-contains
315
+ :end-before: end-filter-contains
316
+ :language: python
235
317
318
+ .. output::
319
+
320
+ <QuerySet [<Movie: Murmur of the Heart>, <Movie: Desert Bloom>,
321
+ <Movie: Girls Town>, <Movie: Get Real>, <Movie: Man of Steel>,
322
+ <Movie: The Holy Land>, <Movie: Secondhand Lions>, <Movie: How to Be Louise>,
323
+ <Movie: Mouth to Mouth>, <Movie: Che ne sarè di noi>, <Movie: Roll Bounce>,
324
+ <Movie: Quando sei nato non puoi piè nasconderti>,
325
+ <Movie: Hey Hey It's Esther Blueburger>, <Movie: A Guide to Recognizing Your Saints>,
326
+ <Movie: Archie's Final Project>, <Movie: Persepolis>, <Movie: The Runaways>,
327
+ <Movie: An Education>, <Movie: SoulBoy>, <Movie: The French Kissers>,
328
+ '...(remaining elements truncated)...']>
236
329
237
330
.. _django-query-comparison:
238
331
@@ -278,18 +371,33 @@ The following example uses the ``__lte`` lookup to
278
371
query for documents in which the ``runtime`` value
279
372
is less than or equal to ``50``:
280
373
374
+ .. io-code-block::
375
+ :copyable:
376
+
377
+ .. input:: /includes/interact-data/specify-a-query.py
378
+ :start-after: start-filter-lte
379
+ :end-before: end-filter-lte
380
+ :language: python
381
+
382
+ .. output::
383
+
384
+ <QuerySet [<Movie: The Great Train Robbery>, <Movie: A Corner in Wheat>,
385
+ <Movie: Winsor McCay, the Famous Cartoonist of the N.Y. Herald and His Moving Comics>,
386
+ <Movie: Gertie the Dinosaur>, <Movie: From Hand to Mouth>,
387
+ <Movie: High and Dizzy>, <Movie: One Week>, <Movie: Now or Never>,
388
+ <Movie: Steamboat Willie>, <Movie: The Music Box>, <Movie: Three Little Pigs>,
389
+ <Movie: The Band Concert>, <Movie: Who Killed Cock Robin?>,
390
+ <Movie: Popeye the Sailor Meets Sindbad the Sailor>, <Movie: Dots>, <Movie: The Land>,
391
+ <Movie: Meshes of the Afternoon>, <Movie: The Memphis Belle: A Story of a Flying Fortress>,
392
+ <Movie: Report from the Aleutians>, <Movie: Saludos Amigos>,
393
+ '...(remaining elements truncated)...']>
281
394
282
395
.. _django-query-span-relationships:
283
396
284
397
Filter Across Relationships
285
398
~~~~~~~~~~~~~~~~~~~~~~~~~~~
286
399
287
- This example retrieves all ``Entry`` objects with a ``Blog`` whose ``name``
288
- is ``'Beatles Blog'``:
289
-
290
- .. code-block:: python
291
-
292
- Entry.objects.filter(blog__name="Beatles Blog")
400
+ This example
293
401
294
402
Modify Query Results
295
403
--------------------
0 commit comments