Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connecting Django +1.10 with MongoDB

Does anybody replaced the default database engine in Django (+1.10) for MongoDB in the last months? All the info I get in Google is like 6 or 4 years ago.

The most commons results includes mongodb-engine which requires django-nonrel (a really old fork from Django 1.5), or mongoengine, also an outdated library that give many errors, so when you patch one, you get a new one.

I don't know if there is any "new" way to accomplish this.

like image 590
vtisnado Avatar asked Feb 11 '17 16:02

vtisnado


People also ask

Can Django work with NoSQL?

Does Django support NoSQL databases? ¶ NoSQL databases are not officially supported by Django itself. There are, however, a number of side projects and forks which allow NoSQL functionality in Django.

Which DB is best for Django?

The three most widely used Database Management Systems for Django are SQLite, MySQL, and PostgreSQL. The Django community and official Django documentation state PostgreSQL as the preferred database for Django Web Apps.

Is Django and MongoDB same?

Django is a SQL to mongodb query transpiler. Using django we can use MongoDB as a backend database for our Django project. We don't even need to change the Django ORM.


1 Answers

With several builds and tests using docker to try different combinations of versions, I managed to reach to a stable and working Django MongoDB app using the following versions:

Django==1.10.0
pymongo==2.7.1
six==1.10.0
mongoengine==0.9.0

After installing the needed libraries, edit your settings.py to update or add the following

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.dummy'
    }
}
AUTHENTICATION_BACKENDS = (
    'mongoengine.django.auth.MongoEngineBackend',
)
from mongoengine import *
    connect('database_name', host='host_ip',  port=27017)

Change the database name and host ip. It tries to connect to localhost by default if no ip is stated.

Now, running your app should have the database connected and ready for use. Check mongoengine documentation for information on how to use it.

like image 126
Husseny Avatar answered Oct 15 '22 08:10

Husseny