简体   繁体   中英

Django with MongoDB

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.

以防万一,情况已经发生变化,现在对此问题有一个解决方案,即django-mongoadmin

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

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 :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM