Django Mongoengine unable to connect to MongoDB

2019-09-10 11:00发布

问题:

We have build our environment using Django 1.7, Mongoengine 0.8.7, MongoDB 2.4.7 and Python 3.4.

We have installed django-rest-framework , but when we try to run the example of the website, we get the following error:

ImproperlyConfigured at /users/

settings.DATABASES is improperly configured. Please supply the ENGINE value. Check settings documentation for more details.

Request Method:         GET
Request URL:         http://localhost:8000/users/
Django Version:         1.7
Exception Type:         ImproperlyConfigured
Exception Value:         

settings.DATABASES is improperly configured. Please supply the ENGINE value. Check settings documentation for more details.

Exception Location:         /Users/user_name/Development/projects/current_project/lib/python3.4/site-packages/django/db/backends/dummy/base.py in complain, line 18
Python Executable:         /Users/user_name/Development/projects/current_project/bin/uwsgi
Python Version:         3.4.1
Python Path:         

['.',
'',
'/Users/user_name/Development/projects/current_project/lib/python34.zip',
'/Users/user_name/Development/projects/current_project/lib/python3.4',
'/Users/user_name/Development/projects/current_project/lib/python3.4/plat-darwin',
'/Users/user_name/Development/projects/current_project/lib/python3.4/lib-dynload',
'/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4',
'/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/plat-darwin',
'/Users/user_name/Development/projects/current_project/lib/python3.4/site-packages']

This is what we have changed in our settings.py according to the documentation that we have found:

import mongoengine

DATABASES = {
    'default': { 
    'ENGINE': 'django.db.backends.dummy',
  }
}

mongoengine.connect('database_name', username='root', password='password')


SESSION_ENGINE = 'mongoengine.django.sessions'
SESSION_SERIALIZER = 'mongoengine.django.sessions.BSONSerializer'

For what we have seen, if we connect through the mongoengine.connect method the ENGINE parameter should not be ignored, but we are finding this error instead. Anyone knows how we could solve it?

回答1:

I used mongodb setting at the end of settings.py file like this

# Mongodb settings

AUTHENTICATION_BACKENDS = (
    'mongoengine.django.auth.MongoEngineBackend',
)

MONGO_DATABASE_NAME = 'prod'


MONGOENGINE_USER_DOCUMENT = 'mongoengine.django.auth.User'

SESSION_ENGINE = 'mongoengine.django.sessions'
SESSION_SERIALIZER = 'mongoengine.django.sessions.BSONSerializer'

from mongoengine import connect
connect(MONGO_DATABASE_NAME)


回答2:

add this to settings.py:

AUTHENTICATION_BACKENDS = (
    'mongoengine.django.auth.MongoEngineBackend',
)

maybe this help you.



回答3:

After some research, we have found how to solve the issue.

Instead of following the example that can be found on the http://www.django-rest-framework.org/ we have followed the Quickstart guide. Following this guide, we have solved our problem.

Many thanks for all the answers!