diff --git a/django_mongodb_backend/utils.py b/django_mongodb_backend/utils.py index ced60bc8..f8a47cda 100644 --- a/django_mongodb_backend/utils.py +++ b/django_mongodb_backend/utils.py @@ -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. @@ -48,6 +48,9 @@ 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, @@ -55,7 +58,7 @@ def parse_uri(uri, *, db_name=None, test=None): "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"] diff --git a/docs/source/ref/utils.rst b/docs/source/ref/utils.rst index cf13fc9c..9f686582 100644 --- a/docs/source/ref/utils.rst +++ b/docs/source/ref/utils.rst @@ -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. @@ -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 `. But for maximum flexibility, construct :setting:`DATABASES` manually as described in :ref:`configuring-databases-setting`. + + .. versionchanged:: 5.2b2 + + The ``options`` parameter was added. diff --git a/docs/source/releases/5.2.x.rst b/docs/source/releases/5.2.x.rst index cf7b2f1b..6b17b86e 100644 --- a/docs/source/releases/5.2.x.rst +++ b/docs/source/releases/5.2.x.rst @@ -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 ============ diff --git a/tests/backend_/utils/test_parse_uri.py b/tests/backend_/utils/test_parse_uri.py index 3198a463..804c4efc 100644 --- a/tests/backend_/utils/test_parse_uri.py +++ b/tests/backend_/utils/test_parse_uri.py @@ -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"})