Project Setup with Django 1.10, mongodb and Python

2019-02-19 11:58发布

问题:

Initially I have built projects with Django 1.5, django-mongoengine, python 2.7.8. I have started a project with an idea came up to my mind. I want to make it using latest versions of technologies therefore I stick with:

Django 1.10,
python 3.4.3

I want to use mongodb as my database. To run my project I am facing an error when I tried to connect mongodb with Django. Code in the settings.py is given as follow to connect database (The only change I made yet in file):

DATABASES = {
    'default': {
        'ENGINE': 'django_mongodb_engine',
        'HOST': '127.0.0.1',
        'PORT':'27017',
        'NAME': 'Demo',
   }
}

Please help me out to setup project so that I can proceed with my work. Also the environment I have setup for my project is:

Django  1.10.1
django-mongodb-engine   0.6.0   
django-mongodb-engine-py3   0.6.0.1 
django-nonrel-enuff 0.4 
django-toolbox  0.1 
djangotoolbox   1.8.0
mongoengine 0.10.6
pip 8.1.2
pymongo 3.3.0
setuptools  27.2.0

Error that I am facing is:

File "/home/username/Documents/Projects /ProjectsENV/lib/python3.4/site-  packages/django_mongodb_engine/base.py", line 272
   raise ImproperlyConfigured, exc_info[1], exc_info[2]
                          ^
SyntaxError: invalid syntax

回答1:

Expanding on the above answer. You can actually use Django with MongoDB directly.

It is possible to use django AS IS with MongoDB as your backend djongo

It is a compiler that converts SQL queries into mongodb syntax. Using this all Django features work as is.

Disclosure: I have contributed to this project and all Django contrib models work fine with this as the backend connector.



回答2:

You can't use the normal Django with django-mongodb-engine.

django-mongodb-engine requires the Django-nonrel package to be installed, instead of the normal Django, which is several minor versions behind the current release of the official Django (1.10).

If you want to use the official django with MongoDB and be provided with an ORM like layer, your best bet is probably Mongoengine which works with the official version of Django.