-->

Django with MongoDB

2019-06-08 03:56发布

问题:

OK.. i am starting a project in django 1.4 and i want MongoDB as my backend. after a half a day of google search, i figured out that mongoengine is a best option(as it is an active project and provides a django like orm)

Now the problem is 1. I cant find any good step-by-step setup guide to integrate mongoengine with a django project.

  1. I understand, using mongoengine means that i am replacing django orm and there is no need to do syncdb. now this project have a multi-tenant architecture (*.domain.com) which i am gonna resolve using a middleware..also a considerable part of this project will work on django admin. Question: will replacing django orm with mongoengine going to affect django admin and other operations(such as middleware, authentication etc.) ?

I am open to suggestions and criticism as well.

回答1:

Django Admin is designed to work with the Django ORM only. Using MongoEngine and no Django ORM will mean you don't get the automatic admin interface. Other middleware might use the Django ORM or be sufficiently abstracted enough to allow you to plugin MongoEngine - eg: Sessions and Authentication.

There are some helpers for Django in MongoEngine - but its by no means complete or designed to be a drop in replacement for the Django ORM.

For more information see this presentation from Django Conf Finland: http://staltz.github.io/djangoconfi-mongoengine



回答2:

Just in case, situation has changed and there is a solution now for this problem, namely django-mongoadmin.



回答3:

A Guide to integrating Django with MongoDB

The way to connect Django with MongoDB by adding just one line of code:

First install djongo:

pip install djongo

Then run your migrations:

manage.py make migrations
manage.py migrate

and finally add to your settings file:

DATABASES = {
   ‘default’: {
      ‘ENGINE’: ‘djongo’,
      ‘NAME’: ‘your-db-name’,
   }
}

It is as simple as that!

If you want to manipulate MongoDB using Django Admin, simply fire it up:

manage.py runserver

Goto: http://localhost:8000/admin/

Manipulate your embedded models as shown in this screenshot:

For more information do checkout the djongo documentation.

You should definitely consider the pros and cons of using a NEW framework (like MongoEngine) vs using inbuilt Django ORM. Do read at this tutorial before considering to adopt MongoEngine as suggested by other knowledgeable members! No offence!

Let me know if you agree with this approach in the comments :)