-
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 8 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,280 @@ | ||||||||||||||||||
.. _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 | ||||||||||||||||||
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 run these operations, call ``QuerySet`` methods | ||||||||||||||||||
|
||||||||||||||||||
your model's ``Manager``. The ``Manager`` class handles database | ||||||||||||||||||
|
||||||||||||||||||
operations and allows you to interact with your MongoDB data by referencing | ||||||||||||||||||
Django models. By default, Django adds a ``Manager`` named ``objects`` | ||||||||||||||||||
|
||||||||||||||||||
to every model class. | ||||||||||||||||||
|
||||||||||||||||||
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>` | ||||||||||||||||||
|
- :ref:`create() <django-crud-insert>` | |
- :ref:`filter() and get() <django-crud-read>` | |
- :ref:`update() <django-crud-modify>` | |
- :ref:`delete() <django-crud-delete>` | |
- :ref:`create() <django-crud-insert>`: Insert new data | |
- :ref:`filter() and get() <django-crud-read>`: Retrieve specific documents... etc | |
- :ref:`update() <django-crud-modify>` | |
- :ref:`delete() <django-crud-delete>` |
Outdated
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.
To learn more about Django's ``QuerySet`` API, see | |
To learn more about Django's ``QuerySet`` API, see the |
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.
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 comment
The 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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
@norareidy were we able to get resolve the issue with sphinx?
Outdated
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.
S: to get around how weird the capitalization looks here
more information, see `The Django admin site <https://docs.djangoproject.com/en/5.1/ref/contrib/admin/>`__ | |
in the Django documentation. | |
more information, see the `Django Admin Site <https://docs.djangoproject.com/en/5.1/ref/contrib/admin/>`__ | |
entry in the Django documentation. |
rustagir marked this conversation as resolved.
Show resolved
Hide resolved
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.
Could you add a helpful tooltip or note to explain the purpose of these two meta configurations. A blurb along the lines of:
"""
In Django, defining a Meta class within a Model allows you to configure the Meta options for the model. In this example, we use db_table = "movies"
to change the collection referenced in the database to look for movies
, and we set managed = False
to stop database creation, modification, or deletion during migration commands. To read more, check out Django's Meta Options documentation.
"""
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.
Since I use this sample data on almost every page, what do you think of linking to the Models guide (which explains Meta and str()) instead of repeating the explanation every time?
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.
Sounds good to me
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.
Same here. Another helpful tooltip:
"""
In Django, you can override the str method of a model to define how its instances are represented as strings.
"""
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.
same comment as above
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.
Yep. Works for me
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.
Note: link broken until #132 is merged
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.
To insert a document into a collection, call the create()
method on your model's Manager
class. Pass the new document's field names and field values as arguments to the create()
method.
Outdated
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.
The ``create()`` method allows you to create a new ``Movie`` object | |
and save the object as a collection document in one method call. | |
The ``create()`` method allows you to create a new ``Movie`` object | |
and save the object to MongoDB in one method call. |
Outdated
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.
I don't know if you want to mention this shortcut here, but I think you can also do this:
movie = Movie(title="Poor Things", runtime=141)
Presumably this works because instantiation of the class calls the manager's create
method or something like that.
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.
Looks like you still need to call save() after to actually insert it into the database
Outdated
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.
Q: why not include this example here?
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.
Added
Outdated
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.
To retrieve documents from your collection, call the filter()
method on your model's Manager
class.
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.
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 comment
The 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 comment
The 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!
Outdated
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.
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!
Outdated
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.
Q: fix indentation?
Uh oh!
There was an error while loading. Please reload this page.