Skip to content
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: 7 additions & 0 deletions django_mongodb_backend/utils.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import copy
import time
import warnings

import django
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured, ValidationError
from django.db.backends.utils import logger
from django.utils.deprecation import RemovedInDjango60Warning
from django.utils.functional import SimpleLazyObject
from django.utils.text import format_lazy
from django.utils.version import get_version_tuple
Expand Down Expand Up @@ -33,6 +35,11 @@ def parse_uri(uri, *, db_name=None, options=None, test=None):
Convert the given uri into a dictionary suitable for Django's DATABASES
setting.
"""
warnings.warn(
'parse_uri() is deprecated. Put the connection string in DATABASES["HOST"] instead.',
RemovedInDjango60Warning,
stacklevel=2,
)
uri = pymongo_parse_uri(uri)
host = None
port = None
Expand Down
27 changes: 27 additions & 0 deletions docs/internals/deprecation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,30 @@ Deprecation Timeline

This document outlines when various pieces of Django MongoDB Backend will be
removed or altered in a backward incompatible way, following their deprecation.

6.0
---

.. _parse-uri-deprecation:

``parse_uri()`` will be removed
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

``django_mongodb_backend.utils.parse_uri()`` is deprecated in favor of putting
the connection string in ``DATABASES["HOST"]``.

For example, instead of::

DATABASES = {
"default": django_mongodb_backend.parse_uri("mongodb://localhost:27017/", db_name="db"),
}

use::

DATABASES = {
'default': {
'ENGINE': 'django_mongodb_backend',
'HOST': 'mongodb://localhost:27017/',
Copy link
Collaborator

@aclark4life aclark4life Sep 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did I notice correctly recently that HOST can be omitted and localhost is chosen by default? If so, probably OK? If not, never mind.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct. I raised that at: mongodb-labs/django-mongodb-project#15 (comment). This example is the old template and the new one. We could replace "localhost:27017" with "..." if that seems better.

Copy link
Collaborator

@aclark4life aclark4life Sep 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it, cool. I'd say let's leave it for now since we're trying to emphasize HOST=URI. Maybe we'll update the project template later to only include the ENGINE and document the other options.

'NAME': 'db',
},
}
6 changes: 6 additions & 0 deletions docs/ref/utils.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ following parts can be considered stable.

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

.. deprecated:: 5.2.2

``parse_uri()`` is deprecated in favor of putting the connection string
in ``DATABASES["HOST"]``. See :ref:`the deprecation timeline
<parse-uri-deprecation>` for upgrade instructions.

Parses a MongoDB `connection string`_ into a dictionary suitable for
Django's :setting:`DATABASES` setting.

Expand Down
8 changes: 8 additions & 0 deletions docs/releases/5.2.x.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ Bug fixes

- ...

Deprecated features
-------------------

- ``django_mongodb_backend.utils.parse_uri()`` is deprecated in favor of
putting the connection string in ``DATABASES["HOST"]``. See
:ref:`the deprecation timeline <parse-uri-deprecation>` for upgrade
instructions.

5.2.1
=====

Expand Down
10 changes: 10 additions & 0 deletions tests/backend_/utils/test_parse_uri.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
import pymongo
from django.core.exceptions import ImproperlyConfigured
from django.test import SimpleTestCase
from django.test.utils import ignore_warnings
from django.utils.deprecation import RemovedInDjango60Warning

from django_mongodb_backend import parse_uri


@ignore_warnings(category=RemovedInDjango60Warning)
class ParseURITests(SimpleTestCase):
def test_simple_uri(self):
settings_dict = parse_uri("mongodb://cluster0.example.mongodb.net/myDatabase")
Expand Down Expand Up @@ -105,3 +108,10 @@ def test_invalid_credentials(self):
def test_no_scheme(self):
with self.assertRaisesMessage(pymongo.errors.InvalidURI, "Invalid URI scheme"):
parse_uri("cluster0.example.mongodb.net")


class ParseURIDeprecationTests(SimpleTestCase):
def test_message(self):
msg = 'parse_uri() is deprecated. Put the connection string in DATABASES["HOST"] instead.'
with self.assertRaisesMessage(RemovedInDjango60Warning, msg):
parse_uri("mongodb://cluster0.example.mongodb.net/")
Loading