Search code examples
pythondjangomongodb

How to connect Django with mongodb Atlas?


I'm creating a Django connection with mongodb Atlas, but I get the following error message when using the makemigrations command:

django==4.0
Python==3.10.12
djongo==1.3.6
pymongo==4.7.3

#! settings.py

DATABASES = {
        'default': {
            'ENGINE': 'djongo',
            'NAME': 'syscandb',
            'ENFORCE_SCHEMA': False,
            'CLIENT': {
                'host': 'mongodb+srv://<user>:<senha>@clusterak.mp9ylv1.mongodb.net/syscandb?retryWrites=true&w=majority'
            }  
        }
}

In MOngo Atlas it guides like this:

mongodb+srv://<username>:<password>@clusterak.mp9ylv1.mongodb.net/?retryWrites=true&w=majority&appName=clusterak

I tested successfully via python like this:

"mongodb+srv://<username>:<password>@clusterak.mp9ylv1.mongodb.net/?retryWrites=true&w=majority&authSource=admin"








(venv) rkart@rkart-B-ON:/home/jobs/micakes/syscan$ python3 manage.py makemigrations
    No changes detected
    Traceback (most recent call last):
      File "/home/jobs/micakes/syscan/manage.py", line 24, in <module>
        main()
      File "/home/jobs/micakes/syscan/manage.py", line 20, in main
        execute_from_command_line(sys.argv)
      File "/home/jobs/micakes/syscan/venv/lib/python3.10/site-packages/django/core/management/__init__.py", line 425, in execute_from_command_line
        utility.execute()
      File "/home/jobs/micakes/syscan/venv/lib/python3.10/site-packages/django/core/management/__init__.py", line 419, in execute
        self.fetch_command(subcommand).run_from_argv(self.argv)
      File "/home/jobs/micakes/syscan/venv/lib/python3.10/site-packages/django/core/management/base.py", line 386, in run_from_argv
        connections.close_all()
      File "/home/jobs/micakes/syscan/venv/lib/python3.10/site-packages/django/db/utils.py", line 213, in close_all
        connection.close()
      File "/home/jobs/micakes/syscan/venv/lib/python3.10/site-packages/django/utils/asyncio.py", line 25, in inner
        return func(*args, **kwargs)
      File "/home/jobs/micakes/syscan/venv/lib/python3.10/site-packages/django/db/backends/base/base.py", line 305, in close
        self._close()
      File "/home/jobs/micakes/syscan/venv/lib/python3.10/site-packages/djongo/base.py", line 208, in _close
        if self.connection:
      File "/home/jobs/micakes/syscan/venv/lib/python3.10/site-packages/pymongo/database.py", line 1342, in __bool__
        raise NotImplementedError(
    NotImplementedError: Database objects do not implement truth value testing or bool(). Please compare with None instead: database is not None

Solution

  • There's an issue when Django tries to close the database connection. Specifically, it seems that djongo is trying to evaluate the truthiness of a pymongo database object, which raises a NotImplementedError.

    The error message advises that instead of using if self.connection, the code should use if self.connection is not None.

    You can try and open the djongo/base.py file and find the _close method. Modify the if self.connection: line to: if self.connection is not None:

    After making these changes, try running the makemigrations command again