Skip to content

Commit 8e5ef5e

Browse files
committed
more info
1 parent 86257f0 commit 8e5ef5e

File tree

2 files changed

+137
-12
lines changed

2 files changed

+137
-12
lines changed

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,21 @@ class Meta:
2727
# end-models
2828

2929
# start-all
30-
# end-all
30+
Movie.objects.all()
31+
# end-all
32+
33+
# start-filter
34+
Movie.objects.filter(runtime=300)
35+
# end-filter
36+
37+
# start-get
38+
Movie.objects.get(title="Finding Nemo")
39+
# end-get
40+
41+
# start-exclude
42+
Movie.objects.exclude(released__lt=timezone.make_aware(datetime(1980, 1, 1)))
43+
# end-exclude
44+
45+
# start-filter-contains
46+
Movie.objects.filter(plot__contains="coming-of-age")
47+
# end-filter-contains

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

Lines changed: 119 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,15 @@ root directory:
6767

6868
python manage.py shell
6969

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+
7079
To learn how to create a Django application that uses the ``Movie``
7180
model and the Python interactive shell to interact with MongoDB documents,
7281
visit the :ref:`django-get-started` tutorial.
@@ -98,6 +107,27 @@ The following example calls the ``all()`` method on the
98107
``Movie`` model to retrieve all documents in the ``sample_mflix.movies``
99108
collection:
100109

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+
101131
.. _django-query-retrieve-matching:
102132

103133
Retrieve Matching Documents
@@ -112,6 +142,18 @@ The following example calls the ``filter()`` method on the
112142
``Movie`` model to query the ``sample_mflix.movies`` collection
113143
for documents that have a ``runtime`` value of ``300``:
114144

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>]>
115157

116158
.. _django-query-retrieve-one:
117159

@@ -135,6 +177,17 @@ The following example calls the ``get()`` method on the
135177
``Movie`` model to retrieve one document that has a ``title``
136178
value of ``"Finding Nemo"``:
137179

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>
138191

139192
.. _django-query-exclude:
140193

@@ -147,12 +200,33 @@ corresponding Django model. Pass the exclusion criteria as an
147200
argument to the ``exclude()`` method.
148201

149202
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)...']>
152226

153227
.. tip::
154228

155-
The preceding example uses the ``gt`` comparison operator
229+
The preceding example uses the ``lt`` comparison operator
156230
to query the collection. To learn more about comparison
157231
operators, see the :ref:`django-query-comparison` section
158232
in this guide.
@@ -230,9 +304,28 @@ Example
230304
```````
231305

232306
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
235317

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)...']>
236329

237330
.. _django-query-comparison:
238331

@@ -278,18 +371,33 @@ The following example uses the ``__lte`` lookup to
278371
query for documents in which the ``runtime`` value
279372
is less than or equal to ``50``:
280373

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)...']>
281394

282395
.. _django-query-span-relationships:
283396

284397
Filter Across Relationships
285398
~~~~~~~~~~~~~~~~~~~~~~~~~~~
286399

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
293401

294402
Modify Query Results
295403
--------------------

0 commit comments

Comments
 (0)