@@ -304,8 +304,8 @@ Example
304
304
```````
305
305
306
306
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"``:
309
309
310
310
.. io-code-block::
311
311
:copyable:
@@ -397,7 +397,76 @@ is less than or equal to ``50``:
397
397
Filter Across Relationships
398
398
~~~~~~~~~~~~~~~~~~~~~~~~~~~
399
399
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)...']>
401
470
402
471
Modify Query Results
403
472
--------------------
@@ -414,16 +483,109 @@ results in the following ways:
414
483
Sort Results
415
484
~~~~~~~~~~~~
416
485
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
+
417
517
.. _django-query-limit:
418
518
419
519
Limit Results
420
520
~~~~~~~~~~~~~
421
521
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
+
422
562
.. _django-query-first:
423
563
424
564
Retrieve the First Result
425
565
~~~~~~~~~~~~~~~~~~~~~~~~~
426
566
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
+
427
589
428
590
Advanced Queries
429
591
----------------
0 commit comments