-
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
DOCSP-46321: CRUD operations #144
Conversation
✅ Deploy Preview for docs-pymongo ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
source/interact-data/crud.txt
Outdated
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 comment
The reason will be displayed to describe this comment to others. Learn more.
@timgraham Do we need managed=False
here too?
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.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Updated
source/interact-data/crud.txt
Outdated
|
||
.. code-block:: bash | ||
|
||
python3 manage.py shell |
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 Can you confirm you want to keep python3
(vs. python
) for MongoDB-wide compliance reasons? Just want to make sure we decide definitively somewhere in writing.
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.
python
is fine, I updated the Getting Started guide as well
snooty.toml
Outdated
@@ -25,6 +25,7 @@ sharedinclude_root = "https://raw.githubusercontent.com/10gen/docs-shared/main/" | |||
driver-short = "PyMongo" | |||
driver-long = "PyMongo, the MongoDB synchronous Python driver," | |||
driver-async = "PyMongo Async" | |||
django-odm = "MongoDB Backend for 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.
@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
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.
LGTM pending minor issues resolution, which may or may not require additional edits.
.. tip:: | ||
|
||
To learn more about Django's ``QuerySet`` API, see | ||
`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 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?
source/interact-data/crud.txt
Outdated
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() |
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.
no need for 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.
No need for MongoManager
or all 4 lines?
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 think MongoManager
source/interact-data/crud.txt
Outdated
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 comment
The reason will be displayed to describe this comment to others. Learn more.
The phrasing "create()
method on your Movie
objects" is used several times. This isn't precisely correct and could give the impression of something like "Movie.create(). "Movie.objects" is a manager and "create" is a queryset method. Rather than get into all of that, you might just say something like, "The following example uses the create() method to insert a document..."
source/interact-data/crud.txt
Outdated
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 |
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
source/interact-data/crud.txt
Outdated
``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 comment
The reason will be displayed to describe this comment to others. Learn more.
methods ^on your
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 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.
source/interact-data/crud.txt
Outdated
and save the object as a collection document in one method call. | ||
To view an example that creates an object then saves it to the | ||
database by calling ``save()``, see `create() <https://docs.djangoproject.com/en/5.1/ref/models/querysets/#create>`__ | ||
in the Django 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.
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
source/interact-data/crud.txt
Outdated
|
||
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 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.
|
||
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 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.
LGTM
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.
a few small things!
source/interact-data/crud.txt
Outdated
CRUD operations. To run these operations, you can call ``QuerySet`` methods | ||
on your model's ``Manager``. The ``Manager`` class handles database |
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.
CRUD operations. To run these operations, you can call ``QuerySet`` methods | |
on your model's ``Manager``. The ``Manager`` class handles database | |
CRUD operations. To run these operations, call ``QuerySet`` methods | |
on your model's ``Manager`` instance? object?. The ``Manager`` class handles database |
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.
@aclark4life you suggested I remove "instance" - is it more accurate to replace with "object" or just keep as is?
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 like Manager
object.
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.
Manager
is a class with QuerySet
methods mixed in: https://github.com/django/django/blob/stable/5.1.x/django/db/models/manager.py#L176.
The default Manager
is called objects
… and it's an attribute of the model class… but yeah, looking at Django's documentation a manager typically referred to as just "manager". It may help to use "manager" instead of Manager
since we're referring to the model's manager and not the Manager
class e.g.: https://docs.djangoproject.com/en/5.1/topics/db/queries/#retrieving-objects
Lastly, I find instance and object confusing for various reasons. I would say if you see Django using those terms go for it, else stick close to how Django describers managers as much as possible.
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.
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.
Okay thank you! I will use "model's manager" as much as possible
source/interact-data/crud.txt
Outdated
CRUD operations. To run these operations, you can call ``QuerySet`` methods | ||
on 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`` |
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: consider making just "Django" a source constant as its a word with a high possibility of typos, IMO
source/interact-data/crud.txt
Outdated
- :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 comment
The reason will be displayed to describe this comment to others. Learn more.
- :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>` |
source/interact-data/crud.txt
Outdated
|
||
.. tip:: | ||
|
||
To learn more about Django's ``QuerySet`` API, see |
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 |
source/interact-data/crud.txt
Outdated
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 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. |
source/interact-data/crud.txt
Outdated
To view an example that creates an object then saves it to the | ||
database by calling ``save()``, see `create() <https://docs.djangoproject.com/en/5.1/ref/models/querysets/#create>`__ | ||
in the Django 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.
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
source/interact-data/crud.txt
Outdated
|
||
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 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!
source/interact-data/crud.txt
Outdated
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Q: fix indentation?
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.
Another great read. Provided some additional feedback. Let me know what you think
class Meta: | ||
db_table = "movies" | ||
managed = False |
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
def __str__(self): | ||
return self.title |
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
<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 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!
source/interact-data/crud.txt
Outdated
CRUD operations. To run these operations, you can call ``QuerySet`` methods | ||
on your model's ``Manager``. The ``Manager`` class handles database |
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 like Manager
object.
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.
lgtm!
Are there instructions on building the docs locally so we can debug intersphinx? I got this far:
|
class Meta: | ||
db_table = "movies" | ||
managed = False |
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
def __str__(self): | ||
return self.title |
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
Moved to the Django repo: mongodb/docs-django#11 |
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.
LGTM
.. tip:: | ||
|
||
To learn more about Django's ``QuerySet`` API, see | ||
`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 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?
Pull Request Info
PR Reviewing Guidelines
JIRA - https://jira.mongodb.org/browse/DOCSP-46321
Staging - https://deploy-preview-144--docs-pymongo.netlify.app/interact-data/crud/
Note: this PR will move to the docs-django repo.
Self-Review Checklist