|
| 1 | +.. _django-specify-query: |
| 2 | + |
| 3 | +=============== |
| 4 | +Specify a Query |
| 5 | +=============== |
| 6 | + |
| 7 | +.. contents:: On this page |
| 8 | + :local: |
| 9 | + :backlinks: none |
| 10 | + :depth: 2 |
| 11 | + :class: singlecol |
| 12 | + |
| 13 | +.. facet:: |
| 14 | + :name: genre |
| 15 | + :values: reference |
| 16 | + |
| 17 | +.. meta:: |
| 18 | + :keywords: expressions, operations, read, filter, code example |
| 19 | + |
| 20 | +Overview |
| 21 | +-------- |
| 22 | + |
| 23 | +In this guide, you can learn how to use {+django-odm+} to specify |
| 24 | +a database query. |
| 25 | + |
| 26 | +You can refine the set of documents that a query returns by creating a |
| 27 | +**query filter**. A query filter is an expression that specifies the search |
| 28 | +criteria MongoDB uses to match documents in a read or write operation. |
| 29 | +In a query filter, you can prompt the driver to search for documents with an |
| 30 | +exact match to your query, or you can compose query filters to express more |
| 31 | +complex matching criteria. |
| 32 | + |
| 33 | +Sample Data |
| 34 | +~~~~~~~~~~~ |
| 35 | + |
| 36 | +The examples in this guide use the ``Movie`` model, which represents |
| 37 | +the ``sample_mflix.movies`` collection from the :atlas:`Atlas sample datasets </sample-data>`. |
| 38 | +The ``Movie`` model class has the following definition: |
| 39 | + |
| 40 | +.. code-block:: python |
| 41 | + |
| 42 | + from django.db import models |
| 43 | + from django_mongodb_backend.fields import EmbeddedModelField, ArrayField |
| 44 | + from django_mongodb_backend.managers import MongoManager |
| 45 | + |
| 46 | + class Movie(models.Model): |
| 47 | + title = models.CharField(max_length=200) |
| 48 | + plot = models.TextField(blank=True) |
| 49 | + runtime = models.IntegerField(default=0) |
| 50 | + released = models.DateTimeField("release date", null=True, blank=True) |
| 51 | + awards = EmbeddedModelField(Award) |
| 52 | + genres = ArrayField(models.CharField(max_length=100), null=True, blank=True) |
| 53 | + objects = MongoManager() |
| 54 | + |
| 55 | + class Meta: |
| 56 | + db_table = "movies" |
| 57 | + managed = False |
| 58 | + |
| 59 | + def __str__(self): |
| 60 | + return self.title |
| 61 | + |
| 62 | +You can use the Python interactive shell to run the code examples. |
| 63 | +To enter the shell, run the following command from your project's |
| 64 | +root directory: |
| 65 | + |
| 66 | +.. code-block:: bash |
| 67 | + |
| 68 | + python manage.py shell |
| 69 | + |
| 70 | +To learn how to create a Django application that uses the ``Movie`` |
| 71 | +model and the Python interactive shell to interact with MongoDB documents, |
| 72 | +visit the :ref:`django-get-started` tutorial. |
| 73 | + |
| 74 | +Overview |
| 75 | +- each query returns new instance of QuerySet class |
| 76 | +- lazy: doesn't run query until you retrieve results |
| 77 | + |
| 78 | +Methods: |
| 79 | +- all() |
| 80 | +- filter() |
| 81 | +- exclude() |
| 82 | +- get() |
| 83 | + |
| 84 | +Modify: |
| 85 | +- order_by() |
| 86 | +- first() |
| 87 | +- limit -- [5:10] |
| 88 | + |
| 89 | +Query filters: |
| 90 | +- field lookups (__lt, __gt, etc; __exact, __iexact; __contains; embedded lookups; __isnull) |
0 commit comments