-
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 5 commits
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 | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,270 @@ | ||||||||||||||||||
.. _django-crud: | ||||||||||||||||||
|
||||||||||||||||||
======================= | ||||||||||||||||||
Perform CRUD Operations | ||||||||||||||||||
======================= | ||||||||||||||||||
|
||||||||||||||||||
.. contents:: On this page | ||||||||||||||||||
:local: | ||||||||||||||||||
:backlinks: none | ||||||||||||||||||
:depth: 2 | ||||||||||||||||||
:class: singlecol | ||||||||||||||||||
|
||||||||||||||||||
.. facet:: | ||||||||||||||||||
:name: genre | ||||||||||||||||||
:values: reference | ||||||||||||||||||
|
||||||||||||||||||
.. meta:: | ||||||||||||||||||
:keywords: insert, modify, read, write, code example | ||||||||||||||||||
|
||||||||||||||||||
Overview | ||||||||||||||||||
--------- | ||||||||||||||||||
|
||||||||||||||||||
In this guide, you can learn how to use {+django-odm+} to run | ||||||||||||||||||
rustagir marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||
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. | ||||||||||||||||||
This guide shows how to use the following ``QuerySet`` methods: | ||||||||||||||||||
|
||||||||||||||||||
- :ref:`create() <django-crud-insert>` | ||||||||||||||||||
- :ref:`filter() and get() <django-crud-read>` | ||||||||||||||||||
- :ref:`update() <django-crud-modify>` | ||||||||||||||||||
- :ref:`delete() <django-crud-delete>` | ||||||||||||||||||
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.
Suggested change
|
||||||||||||||||||
|
||||||||||||||||||
.. tip:: | ||||||||||||||||||
|
||||||||||||||||||
To learn more about Django's ``QuerySet`` API, see | ||||||||||||||||||
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.
Suggested change
|
||||||||||||||||||
`QuerySet API reference <https://docs.djangoproject.com/en/5.1/ref/models/querysets/>`__ | ||||||||||||||||||
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. How about using intersphinx for linking to Django docs? The objects file: http://docs.djangoproject.com/en/5.0/_objects/ I think this would be especially helpful for making sure links stay updated when switching from one Django version to the next. 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. Looks like there is some precedent for that. 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. Looking into this! 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. @norareidy were we able to get resolve the issue with sphinx? |
||||||||||||||||||
in the Django documentation. | ||||||||||||||||||
|
||||||||||||||||||
You can also use the Django admin site to edit your models | ||||||||||||||||||
and their corresponding collections on a web interface. For | ||||||||||||||||||
more information, see `The Django admin site <https://docs.djangoproject.com/en/5.1/ref/contrib/admin/>`__ | ||||||||||||||||||
in the Django documentation. | ||||||||||||||||||
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: to get around how weird the capitalization looks here
Suggested change
|
||||||||||||||||||
|
||||||||||||||||||
Sample Data | ||||||||||||||||||
~~~~~~~~~~~ | ||||||||||||||||||
|
||||||||||||||||||
The examples in this guide use the ``Movie`` model, which represents | ||||||||||||||||||
the ``sample_mflix.movies`` collection from the :atlas:`Atlas sample datasets </sample-data>`. | ||||||||||||||||||
The ``Movie`` model class has the following definition: | ||||||||||||||||||
|
||||||||||||||||||
.. code-block:: python | ||||||||||||||||||
rustagir marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||
|
||||||||||||||||||
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) | ||||||||||||||||||
plot = models.TextField(null=True) | ||||||||||||||||||
runtime = models.IntegerField(default=0) | ||||||||||||||||||
released = models.DateTimeField("release date", null=True) | ||||||||||||||||||
awards = EmbeddedModelField(Award) | ||||||||||||||||||
genres = ArrayField(models.CharField(max_length=100), blank=True) | ||||||||||||||||||
objects = MongoManager() | ||||||||||||||||||
|
||||||||||||||||||
class Meta: | ||||||||||||||||||
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. @timgraham Do we need 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. Well, yes, there is a lot of feedback from #132 that's not yet incorporated here. 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. Updated |
||||||||||||||||||
db_table = "movies" | ||||||||||||||||||
|
||||||||||||||||||
def __str__(self): | ||||||||||||||||||
return self.title | ||||||||||||||||||
|
||||||||||||||||||
You can use the Python interactive shell to run the code examples. | ||||||||||||||||||
To enter the shell, run the following command from your project's | ||||||||||||||||||
root directory: | ||||||||||||||||||
|
||||||||||||||||||
.. code-block:: bash | ||||||||||||||||||
|
||||||||||||||||||
python3 manage.py shell | ||||||||||||||||||
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. @norareidy Can you confirm you want to keep 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 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 |
||||||||||||||||||
|
||||||||||||||||||
.. _django-crud-insert: | ||||||||||||||||||
|
||||||||||||||||||
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. | ||||||||||||||||||
|
||||||||||||||||||
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 | ||||||||||||||||||
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. The phrasing " |
||||||||||||||||||
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 | ||||||||||||||||||
|
||||||||||||||||||
from sample_mflix.models import Movie | ||||||||||||||||||
|
||||||||||||||||||
movie = Movie.objects.create(title="Poor Things", runtime=141) | ||||||||||||||||||
|
||||||||||||||||||
.. note:: | ||||||||||||||||||
|
||||||||||||||||||
The ``create()`` method allows you to create a new ``Movie`` object | ||||||||||||||||||
and save the object as a collection document in one method call. | ||||||||||||||||||
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.
Suggested change
|
||||||||||||||||||
To view an example that creates an object then saves it to the | ||||||||||||||||||
database by calling ``save()``, see the :ref:`django-get-started-write` | ||||||||||||||||||
step of the Getting Started tutorial. | ||||||||||||||||||
|
||||||||||||||||||
.. _django-crud-read: | ||||||||||||||||||
|
||||||||||||||||||
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 | ||||||||||||||||||
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 | ||||||||||||||||||
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 | ||||||||||||||||||
returns ``Movie`` objects that represent movies released on January 1, 2000: | ||||||||||||||||||
|
||||||||||||||||||
.. io-code-block:: | ||||||||||||||||||
:copyable: true | ||||||||||||||||||
|
||||||||||||||||||
.. input:: | ||||||||||||||||||
:language: python | ||||||||||||||||||
|
||||||||||||||||||
from sample_mflix.models import Movie | ||||||||||||||||||
from django.utils import timezone | ||||||||||||||||||
from datetime import datetime | ||||||||||||||||||
|
||||||||||||||||||
Movie.objects.filter(released=timezone.make_aware(datetime(2000, 1, 1, 0, 0), | ||||||||||||||||||
timezone.get_current_timezone())) | ||||||||||||||||||
|
||||||||||||||||||
.. output:: | ||||||||||||||||||
:language: none | ||||||||||||||||||
:visible: false | ||||||||||||||||||
|
||||||||||||||||||
<MongoQuerySet [<Movie: The Bumblebee Flies Anyway>, <Movie: Angels of the Universe>, | ||||||||||||||||||
<Movie: First Person Plural>, <Movie: Just, Melvin: Just Evil>, <Movie: Sound and Fury>, | ||||||||||||||||||
<Movie: Peppermint Candy>]> | ||||||||||||||||||
|
||||||||||||||||||
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. I'm thinking we should add a tooltip to link to the lookup operations documentation in Django. 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. Here's our intersphinx config: https://github.com/mongodb-labs/django-mongodb-backend/blob/main/docs/source/conf.py#L39-L45 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. Do you mean add a link to the Django documentation for each method at the end of the sections? I can do that! |
||||||||||||||||||
Return One Document Example | ||||||||||||||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||||||||||||||||||
|
||||||||||||||||||
To retrieve only one document that matches your query criteria, call the | ||||||||||||||||||
``get()`` method and pass a query filter as an argument. The following example | ||||||||||||||||||
retrieves a document in which the ``title`` value is ``"Boyhood"``: | ||||||||||||||||||
|
||||||||||||||||||
.. io-code-block:: | ||||||||||||||||||
:copyable: true | ||||||||||||||||||
|
||||||||||||||||||
.. input:: | ||||||||||||||||||
:language: python | ||||||||||||||||||
|
||||||||||||||||||
from sample_mflix.models import Movie | ||||||||||||||||||
|
||||||||||||||||||
Movie.objects.get(title="Boyhood") | ||||||||||||||||||
|
||||||||||||||||||
.. output:: | ||||||||||||||||||
:language: none | ||||||||||||||||||
:visible: false | ||||||||||||||||||
|
||||||||||||||||||
<Movie: Boyhood> | ||||||||||||||||||
|
||||||||||||||||||
.. note:: | ||||||||||||||||||
|
||||||||||||||||||
If your query matches no documents or multiple documents, the ``get()`` | ||||||||||||||||||
method generates an error. To retrieve one document from a query | ||||||||||||||||||
that might match multiple, chain the ``first()`` method to ``filter()``. | ||||||||||||||||||
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: you can still link to the specify a query guide in the TODO, but adding a one line example here of chaining first() wouldnt be a bad idea either! |
||||||||||||||||||
|
||||||||||||||||||
.. _django-crud-modify: | ||||||||||||||||||
|
||||||||||||||||||
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, | ||||||||||||||||||
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 | ||||||||||||||||||
a document that has a ``title`` value of ``"High Fidelity"`` and adds a | ||||||||||||||||||
``plot`` field: | ||||||||||||||||||
|
||||||||||||||||||
.. io-code-block:: | ||||||||||||||||||
:copyable: true | ||||||||||||||||||
|
||||||||||||||||||
.. input:: | ||||||||||||||||||
:language: python | ||||||||||||||||||
|
||||||||||||||||||
from sample_mflix.models import Movie | ||||||||||||||||||
|
||||||||||||||||||
Movie.objects.filter(title="High Fidelity").update(plot= | ||||||||||||||||||
"Rob, a record store owner and compulsive list maker, recounts his top five breakups, including the one in progress.") | ||||||||||||||||||
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. Q: fix indentation? |
||||||||||||||||||
|
||||||||||||||||||
.. output:: | ||||||||||||||||||
:language: none | ||||||||||||||||||
:visible: false | ||||||||||||||||||
|
||||||||||||||||||
// Outputs the number of modified documents | ||||||||||||||||||
1 | ||||||||||||||||||
|
||||||||||||||||||
.. _django-crud-delete: | ||||||||||||||||||
|
||||||||||||||||||
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, | ||||||||||||||||||
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 | ||||||||||||||||||
and deletes documents that have a ``runtime`` value of ``5``: | ||||||||||||||||||
|
||||||||||||||||||
.. io-code-block:: | ||||||||||||||||||
:copyable: true | ||||||||||||||||||
|
||||||||||||||||||
.. input:: | ||||||||||||||||||
:language: python | ||||||||||||||||||
|
||||||||||||||||||
from sample_mflix.models import Movie | ||||||||||||||||||
|
||||||||||||||||||
Movie.objects.filter(runtime=5).delete() | ||||||||||||||||||
|
||||||||||||||||||
.. output:: | ||||||||||||||||||
:language: none | ||||||||||||||||||
:visible: false | ||||||||||||||||||
|
||||||||||||||||||
// Outputs the number of deleted documents and objects | ||||||||||||||||||
(16, {'sample_mflix.Movie': 16}) | ||||||||||||||||||
|
||||||||||||||||||
|
||||||||||||||||||
Additional Information | ||||||||||||||||||
---------------------- | ||||||||||||||||||
|
||||||||||||||||||
.. TODO: To learn more about performing read operations, see the Specify a Query guide. | ||||||||||||||||||
|
||||||||||||||||||
To view more create, read, update, and delete examples, see the following | ||||||||||||||||||
steps of the :ref:`django-get-started` tutorial: | ||||||||||||||||||
|
||||||||||||||||||
- :ref:`django-get-started-write` | ||||||||||||||||||
- :ref:`django-get-started-query` |
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.
@norareidy Also here can you confirm we want to use "MongoDB Backend for Django" instead of "Django MongoDB Backend" ? Thank you!
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.
Updated to "Django MongoDB Backend" in all PRs