-
Notifications
You must be signed in to change notification settings - Fork 20
DOCSP-46328: Django connection configuration #142
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 3 commits
e35be75
445573a
49b6d69
5500f74
9a9220b
3b69e3e
f935192
1049049
11d4f2c
90b7c58
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,174 @@ | ||||||||||
.. _django-connection-configuration: | ||||||||||
|
||||||||||
================================== | ||||||||||
Configure Your Database Connection | ||||||||||
================================== | ||||||||||
|
||||||||||
.. facet:: | ||||||||||
:name: genre | ||||||||||
:values: reference | ||||||||||
|
||||||||||
.. meta:: | ||||||||||
:keywords: connection string, URI, server, settings | ||||||||||
|
||||||||||
.. contents:: On this page | ||||||||||
:local: | ||||||||||
:backlinks: none | ||||||||||
:depth: 2 | ||||||||||
:class: singlecol | ||||||||||
|
||||||||||
Overview | ||||||||||
-------- | ||||||||||
|
||||||||||
In this guide, you can learn how to connect your Django project to MongoDB | ||||||||||
and configure the connection. | ||||||||||
|
||||||||||
|
||||||||||
Connection Configuration | ||||||||||
------------------------ | ||||||||||
|
||||||||||
After installing {+django-odm+} and creating a project, you can configure | ||||||||||
your connection to MongoDB in the following ways: | ||||||||||
|
||||||||||
- :ref:`django-connection-configure-manual` by updating your | ||||||||||
project's ``DATABASES`` setting | ||||||||||
- :ref:`django-connection-configure-automatic` by using | ||||||||||
the ``parse_uri()`` method | ||||||||||
|
||||||||||
.. tip:: | ||||||||||
|
||||||||||
To learn how to install the Django ODM and create a | ||||||||||
|
||||||||||
Django project, 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: this link is broken until #132 is merged |
||||||||||
|
||||||||||
.. _django-connection-configure-manual: | ||||||||||
|
||||||||||
Manually Configure Database Settings | ||||||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||||||||||
|
||||||||||
To manually configure your connection to MongoDB, update | ||||||||||
the ``DATABASES`` setting in your project's ``settings.py`` | ||||||||||
file. The ``DATABASES`` setting has a ``default`` key, | ||||||||||
which you must configure to set MongoDB as the default database | ||||||||||
connection. | ||||||||||
|
||||||||||
To configure the ``default`` key, include the following | ||||||||||
fields: | ||||||||||
|
||||||||||
|
||||||||||
.. list-table:: | ||||||||||
:header-rows: 1 | ||||||||||
:widths: 20 80 | ||||||||||
|
||||||||||
* - Field | ||||||||||
- Description | ||||||||||
|
||||||||||
* - **ENGINE** | ||||||||||
- The backend driver to use for the connection. Set this field to ``"django_mongodb_backend"``. | ||||||||||
|
||||||||||
* - **HOST** | ||||||||||
- | Your connection URI. For localhost connections, this field is optional. | ||||||||||
| For SRV connections, you must include a scheme prefix (``mongodb+srv://``). | ||||||||||
| | ||||||||||
| If connecting to a replica set or sharded cluster with multiple hosts, specify | ||||||||||
| each host separated by a comma. | ||||||||||
|
| If connecting to a replica set or sharded cluster with multiple hosts, specify | |
| each host separated by a comma. | |
| To specify more than one host, include all hostnames in one string. Use | |
| a comma to separate each hostname. |
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.
You can add if your connection requires no auth you do not need to provide it
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 you can say the default port is 27017
Check failure on line 87 in source/configure-connection.txt
GitHub Actions / TDBX Vale rules
[vale] reported by reviewdog 🐶
[MongoDB.ConciseTerms] 'more' is preferred over 'additional'.
Raw Output:
{"message": "[MongoDB.ConciseTerms] 'more' is preferred over 'additional'.", "location": {"path": "source/configure-connection.txt", "range": {"start": {"line": 87, "column": 24}}}, "severity": "ERROR"}
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: related to the preceding paragraph: who is the actor in the list? the client, DATABASES
, the
connection, the deployment, the app, or something else? It seems to mix client/app behavior ('Connects to the database' with config behavior ('Sets the connection option...')
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 was thinking DATABASES. I'll change some of the wording
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.
Wow, English discussion going on here. I would say s/variable to connect to/variable used to connect to/
and reserve further English language usage comments. 😄
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: move this to the OPTIONS row above (and take out of tip box to avoid nested admonition)
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: does this literally construct the DATABASES variable in that file with that same structure? or does it just have the same effect on the client/connection?
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 Django MongoDB README says it "constructs a DATABASES setting equivalent to the first example" -- @Jibola is it accurate to say that parse_uri() automatically constructs this setting?
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.
parse_uri
is a convenience method so if it helps to leave it out for clarity's sake then I would do that. That said, DATABASES
has to exist and it has to have the required keys. For example, I can run Django without that setting but if I try to access the admin I get
settings.DATABASES is improperly configured. Please supply the ENGINE value.
Check settings documentation for more details.
Also FWIW my preference for testing is currently: https://github.com/aclark4life/project-templates/blob/main/project_template/project_name/settings/__init__.py#L7 with MONGODB_URI
set by direnv.
And finally, please note we currently use parse_uri
in the project template.
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.
It seems accurate to say it's constructing the DATABASES setting, so I'll leave that in.
Since I'm showing how to configure the same settings as the previous example, I'll leave the MONGODB_URI variable that shows how to 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:
The following example uses the ``parse_uri()`` method to connect | |
to a MongoDB deployment with the same configuration as | |
The following example uses the ``parse_uri()`` method to specify | |
the same connection configuration as |
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.
NIT: s/method/function/
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.
NIT for future version mappings. We will need to have the links point to the correlating version. Right now it points to stable
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.
Can this be "Django MongoDB Backend" or are we required to say "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.
I can change it! updated