@@ -25,9 +25,12 @@ create, read, update, and delete (CRUD) operations on your MongoDB
25
25
collection.
26
26
27
27
You can use methods provided by the Django ``QuerySet`` API to run
28
- CRUD operations. To update documents in your collection, call the
29
- ``QuerySet`` operation methods on your model objects that represent the collection.
30
- Then, {+django-odm+} runs the operations on your collection documents.
28
+ CRUD operations. To run these operations, call ``QuerySet`` methods
29
+ your model's ``Manager``. The ``Manager`` class handles database
30
+ operations and allows you to interact with your MongoDB data by referencing
31
+ Django models. By default, Django adds a ``Manager`` named ``objects``
32
+ to every model class.
33
+
31
34
This guide shows how to use the following ``QuerySet`` methods:
32
35
33
36
- :ref:`create() <django-crud-insert>`
@@ -57,7 +60,6 @@ The ``Movie`` model class has the following definition:
57
60
58
61
from django.db import models
59
62
from django_mongodb_backend.fields import EmbeddedModelField, ArrayField
60
- from django_mongodb_backend.managers import MongoManager
61
63
62
64
class Movie(models.Model):
63
65
title = models.CharField(max_length=200)
@@ -66,7 +68,6 @@ The ``Movie`` model class has the following definition:
66
68
released = models.DateTimeField("release date", null=True, blank=True)
67
69
awards = EmbeddedModelField(Award)
68
70
genres = ArrayField(models.CharField(max_length=100), null=True, blank=True)
69
- objects = MongoManager()
70
71
71
72
class Meta:
72
73
db_table = "movies"
@@ -83,6 +84,15 @@ root directory:
83
84
84
85
python manage.py shell
85
86
87
+ After entering the Python shell, ensure that you import the following models and
88
+ modules:
89
+
90
+ .. code-block:: python
91
+
92
+ from <your application name>.models import Movie
93
+ from django.utils import timezone
94
+ from datetime import datetime
95
+
86
96
To learn how to create a Django application that uses the ``Movie``
87
97
model and the Python interactive shell to interact with MongoDB documents,
88
98
visit the :ref:`django-get-started` tutorial.
@@ -92,16 +102,16 @@ visit the :ref:`django-get-started` tutorial.
92
102
Insert Documents
93
103
----------------
94
104
95
- To insert a document into a collection, call the ``create()`` method on your
96
- model objects that represent the collection . Pass the new document's fields
97
- and values as arguments to the ``create()`` method.
105
+ To insert a document into a collection, call the ``create()`` method
106
+ on an instance of your model's ``Manager`` class . Pass the new document's
107
+ fields and values as arguments to the ``create()`` method.
98
108
99
109
Example
100
110
~~~~~~~
101
111
102
- The following example calls the ``create()`` method on your ``Movie`` objects
103
- to insert a document into the ``sample_mflix.movies`` collection. The new
104
- document has a ``title`` value of ``"Poor Things"`` and a ``runtime`` value
112
+ The following example calls the ``create()`` method to insert a document
113
+ into the ``sample_mflix.movies`` collection. The new document has
114
+ a ``title`` value of ``"Poor Things"`` and a ``runtime`` value
105
115
of ``141``:
106
116
107
117
.. code-block:: python
@@ -123,8 +133,8 @@ of ``141``:
123
133
Read Documents
124
134
--------------
125
135
126
- To retrieve documents from your collection, call the ``filter()`` method on your
127
- model objects that represent the collection . Pass a query filter, or criteria
136
+ To retrieve documents from your collection, call the ``filter()`` method on an
137
+ instance of your model's ``Manager`` class . Pass a query filter, or criteria
128
138
that specifies which documents to retrieve, as an argument to the ``filter()`` method.
129
139
130
140
Alternatively, you can call the ``get()`` method to retrieve a single document
@@ -133,8 +143,8 @@ that matches your query.
133
143
Return Multiple Documents Example
134
144
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
135
145
136
- The following example calls the ``filter()`` method on your ``Movie`` objects
137
- to retrieve documents from the ``sample_mflix.movies`` collection. The query
146
+ The following example calls the ``filter()`` method to retrieve
147
+ documents from the ``sample_mflix.movies`` collection. The query
138
148
returns ``Movie`` objects that represent movies released on January 1, 2000:
139
149
140
150
.. io-code-block::
@@ -192,16 +202,16 @@ Modify Documents
192
202
----------------
193
203
194
204
To modify documents in a collection, call the ``filter()`` and ``update()``
195
- methods on your model objects that represent the collection . Pass a query filter,
205
+ methods on an instance of your model's ``Manager`` class . Pass a query filter,
196
206
or criteria that specifies which documents to update, as an argument to the
197
207
``filter()`` method. Then, pass the fields and values you want to update as
198
208
arguments to the ``update()`` method.
199
209
200
210
Example
201
211
~~~~~~~
202
212
203
- The following example calls the ``update()`` method on your ``Movie`` objects
204
- to modify documents in the ``sample_mflix.movies`` collection. The code matches
213
+ The following example calls the ``update()`` method to modify
214
+ documents in the ``sample_mflix.movies`` collection. The code matches
205
215
a document that has a ``title`` value of ``"High Fidelity"`` and adds a
206
216
``plot`` field:
207
217
@@ -229,15 +239,15 @@ Delete Documents
229
239
----------------
230
240
231
241
To delete documents in a collection, call the ``filter()`` and ``delete()``
232
- methods on your model objects that represent the collection . Pass a query filter,
242
+ methods on an instance of your model's ``Manager`` class . Pass a query filter,
233
243
or criteria that specifies which documents to delete, as an argument to the
234
244
``filter()`` method.
235
245
236
246
Example
237
247
~~~~~~~
238
248
239
- The following example calls the ``delete()`` method on your ``Movie`` objects
240
- to delete documents in the ``sample_mflix.movies`` collection. The code matches
249
+ The following example calls the ``delete()`` method to delete documents
250
+ in the ``sample_mflix.movies`` collection. The code matches
241
251
and deletes documents that have a ``runtime`` value of ``5``:
242
252
243
253
.. io-code-block::
0 commit comments