-
Notifications
You must be signed in to change notification settings - Fork 20
DOCSP-46321: CRUD operations #144
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
4230372
924788d
6123720
84bc486
b81892b
158ff3d
da3ac8c
15fee18
ce6bcc4
1283d40
073c29c
3861026
9c34109
13fbf87
b3bfeb8
5ba56dc
9636386
6b99b50
64f1e0b
8999776
df740e8
73fe484
60cc381
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,9 +25,12 @@ create, read, update, and delete (CRUD) operations on your MongoDB | |
collection. | ||
|
||
You can use methods provided by the Django ``QuerySet`` API to run | ||
CRUD operations. To update documents in your collection, call the | ||
``QuerySet`` operation methods on your model objects that represent the collection. | ||
Then, {+django-odm+} runs the operations on your collection documents. | ||
CRUD operations. To run these operations, call ``QuerySet`` methods | ||
your model's ``Manager``. The ``Manager`` class handles database | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. methods ^on your |
||
operations and allows you to interact with your MongoDB data by referencing | ||
Django models. By default, Django adds a ``Manager`` named ``objects`` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. S: consider making just "Django" a source constant as its a word with a high possibility of typos, IMO |
||
to every model class. | ||
|
||
This guide shows how to use the following ``QuerySet`` methods: | ||
|
||
- :ref:`create() <django-crud-insert>` | ||
|
@@ -57,7 +60,6 @@ The ``Movie`` model class has the following definition: | |
|
||
from django.db import models | ||
from django_mongodb_backend.fields import EmbeddedModelField, ArrayField | ||
from django_mongodb_backend.managers import MongoManager | ||
|
||
class Movie(models.Model): | ||
title = models.CharField(max_length=200) | ||
|
@@ -66,7 +68,6 @@ The ``Movie`` model class has the following definition: | |
released = models.DateTimeField("release date", null=True, blank=True) | ||
awards = EmbeddedModelField(Award) | ||
genres = ArrayField(models.CharField(max_length=100), null=True, blank=True) | ||
objects = MongoManager() | ||
|
||
class Meta: | ||
db_table = "movies" | ||
|
@@ -83,6 +84,15 @@ root directory: | |
|
||
python manage.py shell | ||
|
||
After entering the Python shell, ensure that you import the following models and | ||
modules: | ||
|
||
.. code-block:: python | ||
|
||
from <your application name>.models import Movie | ||
from django.utils import timezone | ||
from datetime import datetime | ||
|
||
To learn how to create a Django application that uses the ``Movie`` | ||
model and the Python interactive shell to interact with MongoDB documents, | ||
visit the :ref:`django-get-started` tutorial. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note: link broken until #132 is merged |
||
|
@@ -92,16 +102,16 @@ visit the :ref:`django-get-started` tutorial. | |
Insert Documents | ||
---------------- | ||
|
||
To insert a document into a collection, call the ``create()`` method on your | ||
model objects that represent the collection. Pass the new document's fields | ||
and values as arguments to the ``create()`` method. | ||
To insert a document into a collection, call the ``create()`` method | ||
on an instance of your model's ``Manager`` class. Pass the new document's | ||
fields and values as arguments to the ``create()`` method. | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To insert a document into a collection, call the |
||
Example | ||
~~~~~~~ | ||
|
||
The following example calls the ``create()`` method on your ``Movie`` objects | ||
to insert a document into the ``sample_mflix.movies`` collection. The new | ||
document has a ``title`` value of ``"Poor Things"`` and a ``runtime`` value | ||
The following example calls the ``create()`` method to insert a document | ||
into the ``sample_mflix.movies`` collection. The new document has | ||
a ``title`` value of ``"Poor Things"`` and a ``runtime`` value | ||
of ``141``: | ||
|
||
.. code-block:: python | ||
|
@@ -123,8 +133,8 @@ of ``141``: | |
Read Documents | ||
-------------- | ||
|
||
To retrieve documents from your collection, call the ``filter()`` method on your | ||
model objects that represent the collection. Pass a query filter, or criteria | ||
To retrieve documents from your collection, call the ``filter()`` method on an | ||
instance of your model's ``Manager`` class. Pass a query filter, or criteria | ||
that specifies which documents to retrieve, as an argument to the ``filter()`` method. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To retrieve documents from your collection, call the |
||
|
||
Alternatively, you can call the ``get()`` method to retrieve a single document | ||
|
@@ -133,8 +143,8 @@ that matches your query. | |
Return Multiple Documents Example | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
The following example calls the ``filter()`` method on your ``Movie`` objects | ||
to retrieve documents from the ``sample_mflix.movies`` collection. The query | ||
The following example calls the ``filter()`` method to retrieve | ||
documents from the ``sample_mflix.movies`` collection. The query | ||
returns ``Movie`` objects that represent movies released on January 1, 2000: | ||
|
||
.. io-code-block:: | ||
|
@@ -192,16 +202,16 @@ Modify Documents | |
---------------- | ||
|
||
To modify documents in a collection, call the ``filter()`` and ``update()`` | ||
methods on your model objects that represent the collection. Pass a query filter, | ||
methods on an instance of your model's ``Manager`` class. Pass a query filter, | ||
or criteria that specifies which documents to update, as an argument to the | ||
``filter()`` method. Then, pass the fields and values you want to update as | ||
arguments to the ``update()`` method. | ||
|
||
Example | ||
~~~~~~~ | ||
|
||
The following example calls the ``update()`` method on your ``Movie`` objects | ||
to modify documents in the ``sample_mflix.movies`` collection. The code matches | ||
The following example calls the ``update()`` method to modify | ||
documents in the ``sample_mflix.movies`` collection. The code matches | ||
a document that has a ``title`` value of ``"High Fidelity"`` and adds a | ||
``plot`` field: | ||
|
||
|
@@ -229,15 +239,15 @@ Delete Documents | |
---------------- | ||
|
||
To delete documents in a collection, call the ``filter()`` and ``delete()`` | ||
methods on your model objects that represent the collection. Pass a query filter, | ||
methods on an instance of your model's ``Manager`` class. Pass a query filter, | ||
or criteria that specifies which documents to delete, as an argument to the | ||
``filter()`` method. | ||
|
||
Example | ||
~~~~~~~ | ||
|
||
The following example calls the ``delete()`` method on your ``Movie`` objects | ||
to delete documents in the ``sample_mflix.movies`` collection. The code matches | ||
The following example calls the ``delete()`` method to delete documents | ||
in the ``sample_mflix.movies`` collection. The code matches | ||
and deletes documents that have a ``runtime`` value of ``5``: | ||
|
||
.. io-code-block:: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
operations, ^you can call