Skip to content

INTPYTHON-654 Add options parameter to parse_uri() #326

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

Merged
merged 1 commit into from
Jun 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions django_mongodb_backend/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def check_django_compatability():
)


def parse_uri(uri, *, db_name=None, test=None):
def parse_uri(uri, *, db_name=None, options=None, test=None):
"""
Convert the given uri into a dictionary suitable for Django's DATABASES
setting.
Expand All @@ -48,14 +48,17 @@ def parse_uri(uri, *, db_name=None, test=None):
db_name = db_name or uri["database"]
if not db_name:
raise ImproperlyConfigured("You must provide the db_name parameter.")
opts = uri.get("options")
if options:
opts.update(options)
settings_dict = {
"ENGINE": "django_mongodb_backend",
"NAME": db_name,
"HOST": host,
"PORT": port,
"USER": uri.get("username"),
"PASSWORD": uri.get("password"),
"OPTIONS": uri.get("options"),
"OPTIONS": opts,
}
if "authSource" not in settings_dict["OPTIONS"] and uri["database"]:
settings_dict["OPTIONS"]["authSource"] = uri["database"]
Expand Down
11 changes: 10 additions & 1 deletion docs/source/ref/utils.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ following parts can be considered stable.
``parse_uri()``
===============

.. function:: parse_uri(uri, db_name=None, test=None)
.. function:: parse_uri(uri, db_name=None, options=None, test=None)

Parses a MongoDB `connection string`_ into a dictionary suitable for
Django's :setting:`DATABASES` setting.
Expand All @@ -32,8 +32,17 @@ following parts can be considered stable.
You can use the parameters to customize the resulting :setting:`DATABASES`
setting:

- Use ``options`` to provide a dictionary of parameters to
:class:`~pymongo.mongo_client.MongoClient`. These will be merged with
(and, in the case of duplicates, take precedence over) any options
specified in the URI.

- Use ``test`` to provide a dictionary of settings for test databases in
the format of :setting:`TEST <DATABASE-TEST>`.

But for maximum flexibility, construct :setting:`DATABASES` manually as
described in :ref:`configuring-databases-setting`.

.. versionchanged:: 5.2b2

The ``options`` parameter was added.
2 changes: 2 additions & 0 deletions docs/source/releases/5.2.x.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ New features
------------

- Added subquery support for :class:`~.fields.EmbeddedModelArrayField`.
- Added the ``options`` parameter to
:func:`~django_mongodb_backend.utils.parse_uri`.

5.2.0 beta 1
============
Expand Down
11 changes: 11 additions & 0 deletions tests/backend_/utils/test_parse_uri.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,17 @@ def test_auth_source_in_query_string_overrides_defaultauthdb(self):
self.assertEqual(settings_dict["NAME"], "db")
self.assertEqual(settings_dict["OPTIONS"], {"authSource": "auth"})

def test_options_kwarg(self):
options = {"authSource": "auth", "retryWrites": True}
settings_dict = parse_uri(
"mongodb://cluster0.example.mongodb.net/myDatabase?retryWrites=false&retryReads=true",
options=options,
)
self.assertEqual(
settings_dict["OPTIONS"],
{"authSource": "auth", "retryWrites": True, "retryReads": True},
)

def test_test_kwarg(self):
settings_dict = parse_uri("mongodb://localhost/db", test={"NAME": "test_db"})
self.assertEqual(settings_dict["TEST"], {"NAME": "test_db"})
Expand Down
Loading