|
| 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 | +database. |
| 26 | + |
| 27 | +You can use methods provided by the Django ``QuerySet`` API to run |
| 28 | +CRUD operations. This guide shows how to use the following ``QuerySet`` |
| 29 | +methods: |
| 30 | + |
| 31 | +- :ref:`create() <django-crud-insert>` |
| 32 | +- :ref:`filter() <django-crud-read>` |
| 33 | +- :ref:`update() <django-crud-modify>` |
| 34 | +- :ref:`delete() <django-crud-delete>` |
| 35 | + |
| 36 | +.. tip:: |
| 37 | + |
| 38 | + To learn more about Django's ``QuerySet`` API, see |
| 39 | + `QuerySet API reference <https://docs.djangoproject.com/en/5.1/ref/models/querysets/>`__ |
| 40 | + in the Django documentation. |
| 41 | + |
| 42 | +Sample Data |
| 43 | +~~~~~~~~~~~ |
| 44 | + |
| 45 | +The examples in this guide use the ``Movie`` model, which represents |
| 46 | +the ``sample_mflix.movies`` collection from the :atlas:`Atlas sample datasets </sample-data>`. |
| 47 | +The ``Movie`` model class has the following definition: |
| 48 | + |
| 49 | +.. code-block:: python |
| 50 | + |
| 51 | + from django.db import models |
| 52 | + from django_mongodb_backend.fields import EmbeddedModelField, ArrayField |
| 53 | + from django_mongodb_backend.managers import MongoManager |
| 54 | + |
| 55 | + class Movie(models.Model): |
| 56 | + title = models.CharField(max_length=200) |
| 57 | + plot = models.TextField(null=True) |
| 58 | + runtime = models.IntegerField(default=0) |
| 59 | + released = models.DateTimeField("release date", null=True) |
| 60 | + awards = EmbeddedModelField(Award) |
| 61 | + genres = ArrayField(models.CharField(max_length=100), blank=True) |
| 62 | + objects = MongoManager() |
| 63 | + |
| 64 | + class Meta: |
| 65 | + db_table = "movies" |
| 66 | + |
| 67 | + def __str__(self): |
| 68 | + return self.title |
| 69 | + |
| 70 | +To learn how to create a Django application that uses the ``Movie`` |
| 71 | +model to interact with MongoDB documents, visit the :ref:`django-get-started` |
| 72 | +tutorial. |
| 73 | + |
| 74 | +.. _django-crud-insert: |
| 75 | + |
| 76 | +Insert Documents |
| 77 | +---------------- |
| 78 | + |
| 79 | +To insert a document into a collection, call the ``create()`` method on your |
| 80 | +model objects that represent the collection. Pass the new document's fields |
| 81 | +and values as arguments to the ``create()`` method. |
| 82 | + |
| 83 | +Example |
| 84 | +``````` |
| 85 | + |
| 86 | +The following example calls the ``create()`` method on your ``Movie`` objects |
| 87 | +to insert a document into the ``sample_mflix.movies`` collection. The new |
| 88 | +document has a ``title`` value of ``"Poor Things"`` and a ``runtime`` value |
| 89 | +of ``141``: |
| 90 | + |
| 91 | +.. code-block:: python |
| 92 | + |
| 93 | + from sample_mflix.models import Movie |
| 94 | + |
| 95 | + movie = Movie.objects.create(title="Poor Things", runtime=141) |
| 96 | + |
| 97 | +.. note:: |
| 98 | + |
| 99 | + The ``create()`` method allows you to create a new ``Movie`` object |
| 100 | + and save the object as a collection document in one method call. |
| 101 | + To view an example that creates an object then saves it to the |
| 102 | + database by calling ``save()``, see the :ref:`django-get-started-write` |
| 103 | + step of the Getting Started tutorial. |
| 104 | + |
| 105 | +.. _django-crud-read: |
| 106 | + |
| 107 | +Read Documents |
| 108 | +-------------- |
| 109 | + |
| 110 | +To retrieve documents from your collection, call the ``filter()`` method on your |
| 111 | +model objects that represent the collection. Pass a query filter, or criteria |
| 112 | +that specifies which documents to retrieve, as an argument to the ``filter()`` method. |
| 113 | + |
| 114 | +Alternatively, you can call the ``get()`` method to retrieve a single document |
| 115 | +that matches your query. |
| 116 | + |
| 117 | +.. TODO: (add to the previous paragraph) To view an example that calls the |
| 118 | + ``get()`` method, see the Specify a Query guide. |
| 119 | + |
| 120 | +Example |
| 121 | +``````` |
| 122 | + |
| 123 | +The following example calls the ``filter()`` method on your ``Movie`` objects |
| 124 | +to retrieve documents from the ``sample_mflix.movies`` collection. The query |
| 125 | +returns ``Movie`` objects that represent movies released on January 1, 2000: |
| 126 | + |
| 127 | +.. io-code-block:: |
| 128 | + :copyable: true |
| 129 | + |
| 130 | + .. input:: |
| 131 | + :language: python |
| 132 | + |
| 133 | + from sample_mflix.models import Movie |
| 134 | + from django.utils import timezone |
| 135 | + from datetime import datetime |
| 136 | + |
| 137 | + Movie.objects.filter(released=timezone.make_aware(datetime(2000, 1, 1, 0, 0), |
| 138 | + timezone.get_current_timezone())) |
| 139 | + |
| 140 | + .. output:: |
| 141 | + :language: none |
| 142 | + :visible: false |
| 143 | + |
| 144 | + <MongoQuerySet [<Movie: The Bumblebee Flies Anyway>, <Movie: Angels of the Universe>, |
| 145 | + <Movie: First Person Plural>, <Movie: Just, Melvin: Just Evil>, <Movie: Sound and Fury>, |
| 146 | + <Movie: Peppermint Candy>]> |
| 147 | + |
| 148 | + |
| 149 | +.. _django-crud-modify: |
| 150 | + |
| 151 | +Modify Documents |
| 152 | +---------------- |
| 153 | + |
| 154 | +To modify documents in a collection, call the ``filter()`` and ``update()`` |
| 155 | +methods on your model objects that represent the collection. Pass a query filter, |
| 156 | +or criteria that specifies which documents to update, as an argument to the |
| 157 | +``filter()`` method. Then, pass the fields and values you want to update as |
| 158 | +arguments to the ``update()`` method. |
| 159 | + |
| 160 | +Example |
| 161 | +``````` |
| 162 | + |
| 163 | +The following example calls the ``update()`` method on your ``Movie`` objects |
| 164 | +to modify documents in the ``sample_mflix.movies`` collection. The code matches |
| 165 | +a document that has a ``title`` value of ``"High Fidelity"`` and adds a |
| 166 | +``plot`` field: |
| 167 | + |
| 168 | +.. io-code-block:: |
| 169 | + :copyable: true |
| 170 | + |
| 171 | + .. input:: |
| 172 | + :language: python |
| 173 | + |
| 174 | + from sample_mflix.models import Movie |
| 175 | + |
| 176 | + Movie.objects.filter(title="High Fidelity").update(plot= |
| 177 | + "Rob, a record store owner and compulsive list maker, recounts his top five breakups, including the one in progress.") |
| 178 | + |
| 179 | + .. output:: |
| 180 | + :language: none |
| 181 | + :visible: false |
| 182 | + |
| 183 | + // Outputs the number of modified documents |
| 184 | + 1 |
| 185 | + |
| 186 | +.. _django-crud-delete: |
| 187 | + |
| 188 | +Delete Documents |
| 189 | +---------------- |
| 190 | + |
| 191 | +To delete documents in a collection, call the ``filter()`` and ``delete()`` |
| 192 | +methods on your model objects that represent the collection. Pass a query filter, |
| 193 | +or criteria that specifies which documents to delete, as an argument to the |
| 194 | +``filter()`` method. |
| 195 | + |
| 196 | +Example |
| 197 | +``````` |
| 198 | + |
| 199 | +The following example calls the ``delete()`` method on your ``Movie`` objects |
| 200 | +to delete documents in the ``sample_mflix.movies`` collection. The code matches |
| 201 | +and deletes documents that have a ``runtime`` value of ``5``: |
| 202 | + |
| 203 | +.. io-code-block:: |
| 204 | + :copyable: true |
| 205 | + |
| 206 | + .. input:: |
| 207 | + :language: python |
| 208 | + |
| 209 | + from sample_mflix.models import Movie |
| 210 | + |
| 211 | + Movie.objects.filter(runtime=5).delete() |
| 212 | + |
| 213 | + .. output:: |
| 214 | + :language: none |
| 215 | + :visible: false |
| 216 | + |
| 217 | + // Outputs the number of deleted documents and objects |
| 218 | + 16 |
| 219 | + |
| 220 | + |
| 221 | +Additional Information |
| 222 | +---------------------- |
| 223 | + |
| 224 | +.. TODO: To learn more about performing read operations, see the Specify a Query guide. |
| 225 | + |
| 226 | +To view more create, read, update, and delete examples, see the following |
| 227 | +steps of the :ref:`django-get-started` tutorial: |
| 228 | + |
| 229 | +- :ref:`django-get-started-write` |
| 230 | +- :ref:`django-get-started-read` |
0 commit comments