Skip to content

Commit b5ceafc

Browse files
committed
more
1 parent 8e5ef5e commit b5ceafc

File tree

2 files changed

+198
-12
lines changed

2 files changed

+198
-12
lines changed

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

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22
from django.db import models
33
from django_mongodb_backend.fields import EmbeddedModelField, ArrayField
44

5+
class Award(models.Model):
6+
wins = models.IntegerField(default=0)
7+
nominations = models.IntegerField(default=0)
8+
text = models.CharField(max_length=100)
9+
10+
class Meta:
11+
managed = False
12+
513
class Movie(models.Model):
614
title = models.CharField(max_length=200)
715
plot = models.TextField(blank=True)
@@ -16,14 +24,6 @@ class Meta:
1624

1725
def __str__(self):
1826
return self.title
19-
20-
class Award(models.Model):
21-
wins = models.IntegerField(default=0)
22-
nominations = models.IntegerField(default=0)
23-
text = models.CharField(max_length=100)
24-
25-
class Meta:
26-
managed = False
2727
# end-models
2828

2929
# start-all
@@ -44,4 +44,28 @@ class Meta:
4444

4545
# start-filter-contains
4646
Movie.objects.filter(plot__contains="coming-of-age")
47-
# end-filter-contains
47+
# end-filter-contains
48+
49+
# start-filter-lte
50+
Movie.objects.filter(runtime__lte=50)
51+
# end-filter-lte
52+
53+
# start-filter-relationships
54+
Movie.objects.filter(awards__wins=93)
55+
# end-filter-relationships
56+
57+
# start-filter-combine
58+
Movie.objects.filter(awards__text__istartswith="nominated")
59+
# end-filter-combine
60+
61+
# start-sort
62+
Movie.objects.filter(title__startswith="Rocky").order_by("released")
63+
# end-sort
64+
65+
# start-limit
66+
Movie.objects.filter(released=timezone.make_aware(datetime(2010, 7, 1)))[3:6]
67+
# end-limit
68+
69+
# start-first
70+
Movie.objects.filter(genres=["Crime", "Comedy"]).first()
71+
# end-first

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

Lines changed: 165 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -304,8 +304,8 @@ Example
304304
```````
305305

306306
The following example uses the ``__contains`` lookup to
307-
query for documents in which the ``plot`` field
308-
includes ``"coming-of-age"``:
307+
query for documents in which the ``plot`` value
308+
includes the text ``"coming-of-age"``:
309309

310310
.. io-code-block::
311311
:copyable:
@@ -397,7 +397,76 @@ is less than or equal to ``50``:
397397
Filter Across Relationships
398398
~~~~~~~~~~~~~~~~~~~~~~~~~~~
399399

400-
This example
400+
You can run operations on your model that query fields of related
401+
models. To query across relationships, specify each related field
402+
name separated by double underscores (``__``) until you reach the
403+
field you want to query, as shown in the following code:
404+
405+
.. code-block:: python
406+
:copyable: false
407+
408+
Model.objects.filter(<model field>__<related model field>=<value>)
409+
410+
Example
411+
```````
412+
413+
This example uses a lookup that spans the ``Movie`` and ``Award``
414+
relationship. The ``Movie`` model's ``awards`` field has an
415+
embedded ``Award`` model value, which represents the ``awards``
416+
object field in the MongoDB collection and its nested ``wins``,
417+
``nominations``, and ``text`` fields. The code uses a lookup to query for
418+
documents in which the ``awards.wins`` nested field value is ``93``:
419+
420+
.. io-code-block::
421+
:copyable:
422+
423+
.. input:: /includes/interact-data/specify-a-query.py
424+
:start-after: start-filter-relationships
425+
:end-before: end-filter-relationships
426+
:language: python
427+
428+
.. output::
429+
430+
<QuerySet [<Movie: The Queen>, <Movie: Dallas Buyers Club>]>
431+
432+
.. _django-query-combine:
433+
434+
Combine Lookups
435+
~~~~~~~~~~~~~~~
436+
437+
You can combine field lookups to further specify your
438+
query criteria. To combine lookups, chain each lookup
439+
type together and separate them with a double underscore
440+
(``__``).
441+
442+
Example
443+
```````
444+
445+
The following example combines lookup types to query for documents
446+
in which the value of the ``awards.text`` nested field
447+
begins with the text ``"nominated"``, instructing
448+
{+django-odm+} to perform a case-insensitive text search:
449+
450+
.. io-code-block::
451+
:copyable:
452+
453+
.. input:: /includes/interact-data/specify-a-query.py
454+
:start-after: start-filter-combine
455+
:end-before: end-filter-combine
456+
:language: python
457+
458+
.. output::
459+
460+
<QuerySet [<Movie: Hallelujah>, <Movie: Little Caesar>,
461+
<Movie: Morocco>, <Movie: Romance>, <Movie: The Guardsman>,
462+
<Movie: The Front Page>, <Movie: The Public Enemy>,
463+
<Movie: è Nous la Libertè>, <Movie: Smilin' Through>,
464+
<Movie: Flying Down to Rio>, <Movie: She Done Him Wrong>,
465+
<Movie: State Fair>, <Movie: The Barretts of Wimpole Street>,
466+
<Movie: Imitation of Life>, <Movie: The Lost Patrol>,
467+
<Movie: Alice Adams>, <Movie: Black Fury>, <Movie: David Copperfield>,
468+
<Movie: Les Misèrables>, <Movie: Roberta>,
469+
'...(remaining elements truncated)...']>
401470

402471
Modify Query Results
403472
--------------------
@@ -414,16 +483,109 @@ results in the following ways:
414483
Sort Results
415484
~~~~~~~~~~~~
416485

486+
You can provide a sort order for your query results by using the
487+
``order_by()`` method. To specify an ascending sort based on values
488+
of a given field, pass the field name as an argument. To specify a descending
489+
sort, pass the field name prefixed with a minus symbol (``-``) as an argument.
490+
491+
Example
492+
```````
493+
494+
This example performs the following actions:
495+
496+
- Calls the ``filter()`` method on the ``Movie`` model to query
497+
the ``sample_mflix.movies`` collection
498+
- Queries documents that have a ``title`` value starting
499+
with the text ``"Rocky"``
500+
- Sorts the results in ascending order of their ``released`` field
501+
values
502+
503+
.. io-code-block::
504+
:copyable:
505+
506+
.. input:: /includes/interact-data/specify-a-query.py
507+
:start-after: start-sort
508+
:end-before: end-sort
509+
:language: python
510+
511+
.. output::
512+
513+
<QuerySet [<Movie: Rocky>, <Movie: Rocky II>, <Movie: Rocky III>,
514+
<Movie: Rocky IV>, <Movie: Rocky V>, <Movie: Rocky Marciano>,
515+
<Movie: Rocky Balboa>]>
516+
417517
.. _django-query-limit:
418518

419519
Limit Results
420520
~~~~~~~~~~~~~
421521

522+
You can specify the number of results that a query returns
523+
by using Python's array-slicing syntax, as shown
524+
in the following code:
525+
526+
.. code-block:: python
527+
:copyable: false
528+
529+
Model.objects.filter(<query filter>)[<start>:<end>]
530+
531+
Replace the ``<start>`` and ``<end>`` placeholders with integer
532+
values representing the subset of results you want to return.
533+
The start value is non-inclusive and the end value is inclusive.
534+
535+
If you omit the ``<start>`` value, the query returns each result,
536+
beginning with the first match, until it returns the number specified
537+
by the ``<end>`` value.
538+
539+
Example
540+
```````
541+
542+
This example performs the following actions:
543+
544+
- Calls the ``filter()`` method on the ``Movie`` model to query
545+
the ``sample_mflix.movies`` collection
546+
- Queries documents that have a ``released`` value of
547+
``datetime(2010, 7, 16)`` (July 16, 2010)
548+
- Returns the third and fourth results
549+
550+
.. io-code-block::
551+
:copyable:
552+
553+
.. input:: /includes/interact-data/specify-a-query.py
554+
:start-after: start-limit
555+
:end-before: end-limit
556+
:language: python
557+
558+
.. output::
559+
560+
<QuerySet [<Movie: Inception>, <Movie: Winter's Bone>]>
561+
422562
.. _django-query-first:
423563

424564
Retrieve the First Result
425565
~~~~~~~~~~~~~~~~~~~~~~~~~
426566

567+
To retrieve the first result from a query that might match
568+
multiple results, chain the ``first()`` method to the ``filter()``
569+
method. Pass a query filter to the ``filter()`` method that specifies your
570+
query criteria.
571+
572+
The following example calls the ``filter()`` and ``first()`` methods on the
573+
``Movie`` model to query the ``sample_mflix.movies`` collection
574+
for the first document in which the ``genres`` value is
575+
``["Crime", "Comedy"]``:
576+
577+
.. io-code-block::
578+
:copyable:
579+
580+
.. input:: /includes/interact-data/specify-a-query.py
581+
:start-after: start-first
582+
:end-before: end-first
583+
:language: python
584+
585+
.. output::
586+
587+
<Movie: The Crew>
588+
427589

428590
Advanced Queries
429591
----------------

0 commit comments

Comments
 (0)