Djongo NotImplementedError: Database objects do not implement truth value testing or bool()

Python 3.10.2
Django==4.1.1
PyMongo==4.2.0
djongo==1.3.6
while running migrate
NotImplementedError: Database objects do not implement truth value testing or bool(). Please compare with None instead: database is not None
Reinstall pymongo pymongo==3.12.3 resolved this problem.

3 Likes

I’m using mongodb 6.0.1 Community,maybe should be wating for next version of djongo

Welcome to the MongoDB Community @John_Wang !

The Djongo open source project currently isn’t very actively maintained, so I suspect you’ll be waiting a long while for bug fixes. You might be able to find more advice via the community resources in the Djongo Readme: Djongo Questions and Discussions.

Djongo 1.3.6 was released in June 2021 almost six months before the PyMongo 4.0 driver, so presumably needs some updates for full compatibility. Thanks for sharing your workaround.

Can you share more details on how you are trying to use MongoDB with Django? For example: are you trying to run everything (including Django admin UI) with MongoDB as a backend database?

I would personally be inclined to use an official driver like PyMongo or Motor with one of the Python microframeworks. The Django core is designed around SQL and isn’t a great fit for MongoDB at the moment. Alternatively, you could use a driver like PyMongo in your Django views but would still need a supported relational database for core Django admin features.

Regards,
Stennie

1 Like

To address the original error:

NotImplementedError: Database objects do not implement truth value testing or bool(). Please compare with None instead: database is not None

This was an intentional minor breaking change we made in PyMongo 4.0 described here:

Database.__bool__ raises NotImplementedError¶
Database now raises an error upon evaluating as a Boolean. Code like this:

if database:
Can be changed to this:

if database is not None:
You must now explicitly compare with None.

https://pymongo.readthedocs.io/en/stable/migrate-to-pymongo4.html#database-bool-raises-notimplementederror

The full trackback of the exception will show which line needs to be changed.

2 Likes

it’s resolved
after
pip install pymongo==3.12.3

1 Like

I could fix the line of error in djongo with like “if self.connection ⇒ self.connection is not None”

1 Like

At First I felt helpless, Re installing pyMongo==3.12.3 resolved my problem

1 Like

After install pyMongo==3.12.3.
My problem resolved.

1 Like