|
| 1 | +.. _django-crud: |
| 2 | + |
| 3 | +======================= |
| 4 | +Perform CRUD Operations |
| 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: insert, modify, read, write, code example |
| 19 | + |
| 20 | +Overview |
| 21 | +--------- |
| 22 | + |
| 23 | +In this guide, you can learn how to use {+django-odm+} to run |
| 24 | +create, read, update, and delete (CRUD) operations on your MongoDB |
| 25 | +collection. |
| 26 | + |
| 27 | +You can also use the {+framework+} admin site to edit your models |
| 28 | +and their corresponding collections on a web interface. For |
| 29 | +more information, see the `Django Admin Site <https://docs.djangoproject.com/en/5.1/ref/contrib/admin/>`__ |
| 30 | +entry in the {+framework+} documentation. |
| 31 | + |
| 32 | +Query API |
| 33 | +~~~~~~~~~ |
| 34 | + |
| 35 | +You can use methods provided by the {+framework+} ``QuerySet`` API to run |
| 36 | +CRUD operations. To run these operations, you can call ``QuerySet`` methods |
| 37 | +on your model's manager. The ``Manager`` class handles database |
| 38 | +operations and allows you to interact with your MongoDB data by referencing |
| 39 | +Django models. By default, {+framework+} adds a ``Manager`` named ``objects`` |
| 40 | +to every model class. |
| 41 | + |
| 42 | +.. tip:: |
| 43 | + |
| 44 | + To learn more about {+framework+}'s ``QuerySet`` API, see the |
| 45 | + `QuerySet API reference <https://docs.djangoproject.com/en/5.1/ref/models/querysets/>`__ |
| 46 | + in the {+framework+} documentation. |
| 47 | + |
| 48 | +This guide shows how to use the following ``QuerySet`` methods: |
| 49 | + |
| 50 | +- :ref:`create() <django-crud-insert>`: Inserts documents into the collection |
| 51 | +- :ref:`filter() and get() <django-crud-read>`: Retrieves one or multiple collection documents |
| 52 | +- :ref:`update() <django-crud-modify>`: Modifies collection documents |
| 53 | +- :ref:`delete() <django-crud-delete>`: Deletes collection documents |
| 54 | + |
| 55 | +Sample Data |
| 56 | +~~~~~~~~~~~ |
| 57 | + |
| 58 | +The examples in this guide use the ``Movie`` model, which represents |
| 59 | +the ``sample_mflix.movies`` collection from the :atlas:`Atlas sample datasets </sample-data>`. |
| 60 | +The ``Movie`` model class has the following definition: |
| 61 | + |
| 62 | +.. literalinclude:: /includes/interact-data/crud.py |
| 63 | + :start-after: start-models |
| 64 | + :end-before: end-models |
| 65 | + :language: python |
| 66 | + :copyable: |
| 67 | + |
| 68 | +.. include:: /includes/use-sample-data.rst |
| 69 | + |
| 70 | + .. replacement:: model-classes |
| 71 | + |
| 72 | + ``Movie`` model includes |
| 73 | + |
| 74 | + .. replacement:: model-imports |
| 75 | + |
| 76 | + .. code-block:: python |
| 77 | + |
| 78 | + from <your application name>.models import Movie |
| 79 | + from django.utils import timezone |
| 80 | + from datetime import datetime |
| 81 | + |
| 82 | +.. _django-crud-insert: |
| 83 | + |
| 84 | +Insert Documents |
| 85 | +---------------- |
| 86 | + |
| 87 | +To insert a document into a collection, call the ``create()`` method on your |
| 88 | +model's manager. Pass the new document's field names and field values |
| 89 | +as arguments to the ``create()`` method. |
| 90 | + |
| 91 | +Example |
| 92 | +~~~~~~~ |
| 93 | + |
| 94 | +The following example calls the ``create()`` method to insert a document |
| 95 | +into the ``sample_mflix.movies`` collection. The new document has |
| 96 | +a ``title`` value of ``"Poor Things"`` and a ``runtime`` value |
| 97 | +of ``141``: |
| 98 | + |
| 99 | +.. literalinclude:: /includes/interact-data/crud.py |
| 100 | + :start-after: start-insert |
| 101 | + :end-before: end-insert |
| 102 | + :language: python |
| 103 | + :copyable: |
| 104 | + |
| 105 | +The ``create()`` method allows you to create a new ``Movie`` object |
| 106 | +and save the object to MongoDB in one method call. Alternatively, you |
| 107 | +can create a ``Movie`` object and call ``save()``, as shown in the following |
| 108 | +code: |
| 109 | + |
| 110 | +.. literalinclude:: /includes/interact-data/crud.py |
| 111 | + :start-after: start-save |
| 112 | + :end-before: end-save |
| 113 | + :language: python |
| 114 | + :copyable: |
| 115 | + |
| 116 | +.. tip:: |
| 117 | + |
| 118 | + To learn more about the ``create()`` method, see `create() |
| 119 | + <{+django-docs+}/ref/models/querysets/#create>`__ in the {+framework+} |
| 120 | + documentation. |
| 121 | + |
| 122 | +.. _django-crud-read: |
| 123 | + |
| 124 | +Read Documents |
| 125 | +-------------- |
| 126 | + |
| 127 | +To retrieve documents from your collection, call the ``filter()`` method |
| 128 | +on your model's manager. Pass a query filter, or criteria that specifies |
| 129 | +which documents to retrieve, as an argument to the ``filter()`` method. |
| 130 | + |
| 131 | +Alternatively, you can call the ``get()`` method to retrieve a single document |
| 132 | +that matches your query. |
| 133 | + |
| 134 | +Return Multiple Documents Example |
| 135 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 136 | + |
| 137 | +The following example calls the ``filter()`` method to retrieve |
| 138 | +documents from the ``sample_mflix.movies`` collection. The query |
| 139 | +returns ``Movie`` objects that represent movies released on January 1, 2000: |
| 140 | + |
| 141 | +.. io-code-block:: |
| 142 | + :copyable: |
| 143 | + |
| 144 | + .. input:: /includes/interact-data/crud.py |
| 145 | + :start-after: start-find-many |
| 146 | + :end-before: end-find-many |
| 147 | + :language: python |
| 148 | + |
| 149 | + .. output:: |
| 150 | + :visible: false |
| 151 | + |
| 152 | + <QuerySet [<Movie: The Bumblebee Flies Anyway>, <Movie: Angels of the Universe>, |
| 153 | + <Movie: First Person Plural>, <Movie: Just, Melvin: Just Evil>, <Movie: Sound and Fury>, |
| 154 | + <Movie: Peppermint Candy>]> |
| 155 | + |
| 156 | +.. tip:: |
| 157 | + |
| 158 | + To learn more about the ``filter()`` method, see `filter() |
| 159 | + <{+django-docs+}/ref/models/querysets/#filter>`__ in the {+framework+} |
| 160 | + documentation. |
| 161 | + |
| 162 | +Return One Document Example |
| 163 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 164 | + |
| 165 | +To retrieve only one document that matches your query criteria, call the |
| 166 | +``get()`` method and pass a query filter as an argument. The following example |
| 167 | +retrieves a document in which the ``title`` value is ``"Boyhood"``: |
| 168 | + |
| 169 | +.. io-code-block:: |
| 170 | + :copyable: |
| 171 | + |
| 172 | + .. input:: /includes/interact-data/crud.py |
| 173 | + :start-after: start-find-one |
| 174 | + :end-before: end-find-one |
| 175 | + :language: python |
| 176 | + |
| 177 | + .. output:: |
| 178 | + :visible: false |
| 179 | + |
| 180 | + <Movie: Boyhood> |
| 181 | + |
| 182 | +.. important:: |
| 183 | + |
| 184 | + If your query matches no documents or multiple documents, the ``get()`` |
| 185 | + method generates an error. To retrieve one document from a query |
| 186 | + that might match multiple, chain the ``first()`` method to ``filter()``, |
| 187 | + as shown in the following code: |
| 188 | + |
| 189 | + .. code-block:: python |
| 190 | + |
| 191 | + Movie.objects.filter(title="Boyhood").first() |
| 192 | + |
| 193 | +.. tip:: |
| 194 | + |
| 195 | + To learn more about the ``get()`` method, see `get() |
| 196 | + <{+django-docs+}/ref/models/querysets/#get>`__ in the {+framework+} |
| 197 | + documentation. |
| 198 | + |
| 199 | +.. _django-crud-modify: |
| 200 | + |
| 201 | +Modify Documents |
| 202 | +---------------- |
| 203 | + |
| 204 | +To modify documents in a collection, call the ``filter()`` and ``update()`` |
| 205 | +methods on your model's manager. Pass a query filter, or criteria that |
| 206 | +specifies which documents to update, as an argument to the ``filter()`` |
| 207 | +method. Then, pass the fields and values you want to update as |
| 208 | +arguments to the ``update()`` method. |
| 209 | + |
| 210 | +Example |
| 211 | +~~~~~~~ |
| 212 | + |
| 213 | +The following example calls the ``update()`` method to modify |
| 214 | +documents in the ``sample_mflix.movies`` collection. The code matches |
| 215 | +a document that has a ``title`` value of ``"High Fidelity"`` and adds a |
| 216 | +``plot`` field: |
| 217 | + |
| 218 | +.. io-code-block:: |
| 219 | + :copyable: |
| 220 | + |
| 221 | + .. input:: /includes/interact-data/crud.py |
| 222 | + :start-after: start-update |
| 223 | + :end-before: end-update |
| 224 | + :language: python |
| 225 | + |
| 226 | + .. output:: |
| 227 | + :visible: false |
| 228 | + |
| 229 | + // Outputs the number of modified documents |
| 230 | + 1 |
| 231 | + |
| 232 | +.. tip:: |
| 233 | + |
| 234 | + To learn more about the ``update()`` method, see `update() |
| 235 | + <{+django-docs+}/ref/models/querysets/#update>`__ in the {+framework+} |
| 236 | + documentation. |
| 237 | + |
| 238 | +.. _django-crud-delete: |
| 239 | + |
| 240 | +Delete Documents |
| 241 | +---------------- |
| 242 | + |
| 243 | +To delete documents in a collection, call the ``filter()`` and ``delete()`` |
| 244 | +methods on your model's ``Manager`` class. Pass a query filter, |
| 245 | +or criteria that specifies which documents to delete, as an argument to the |
| 246 | +``filter()`` method. |
| 247 | + |
| 248 | +Example |
| 249 | +~~~~~~~ |
| 250 | + |
| 251 | +The following example calls the ``delete()`` method to delete documents |
| 252 | +in the ``sample_mflix.movies`` collection. The code matches |
| 253 | +and deletes documents that have a ``runtime`` value of ``5``: |
| 254 | + |
| 255 | +.. io-code-block:: |
| 256 | + :copyable: |
| 257 | + |
| 258 | + .. input:: /includes/interact-data/crud.py |
| 259 | + :start-after: start-delete |
| 260 | + :end-before: end-delete |
| 261 | + :language: python |
| 262 | + |
| 263 | + .. output:: |
| 264 | + :visible: false |
| 265 | + |
| 266 | + // Outputs the number of deleted documents and objects |
| 267 | + (16, {'sample_mflix.Movie': 16}) |
| 268 | + |
| 269 | +.. tip:: |
| 270 | + |
| 271 | + To learn more about the ``delete()`` method, see `delete() |
| 272 | + <{+django-docs+}/ref/models/querysets/#delete>`__ in the {+framework+} |
| 273 | + documentation. |
| 274 | + |
| 275 | +Additional Information |
| 276 | +---------------------- |
| 277 | + |
| 278 | +.. TODO: To learn more about performing read operations, see the Specify a Query guide. |
| 279 | + |
| 280 | +To view more create, read, update, and delete examples, see the following |
| 281 | +steps of the :ref:`django-get-started` tutorial: |
| 282 | + |
| 283 | +- :ref:`django-get-started-write` |
| 284 | +- :ref:`django-get-started-query` |
0 commit comments