Welcome to JiKe DevOps Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

python - Mongoengine: ConnectionError: You have not defined a default connection

In my new Django project I set up a MongoDb database and use mongoengine module but I can't properly access to dabase nore in shell no in views. "ConnectionError: You have not defined a default connection"

My settings.py includes the following:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'DaTaBaSe',                      
        'USER': 'root',
        'PASSWORD': '',
        'HOST': 'localhost', 
        'PORT': '3306',                  
    },
    'tracking': {
        'ENGINE': 'django.db.backends.dummy',
        'NAME': 'analytics',
    }
}



import mongoengine
SESSION_ENGINE = 'mongoengine.django.sessions'
mongoengine.connect(_MONGODB_NAME, 'localhost:27017')
AUTHENTICATION_BACKENDS = (
       'mongoengine.django.auth.MongoEngineBackend',
        )

in models.py import mongoengine

from mongoengine import *
from myproject.settings import _MONGODB_NAME
mongoengine.connect(_MONGODB_NAME, 'localhost:27017')

Thanks you in advance for your help

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

Please log in or register to answer this question.

1 Answer

0 votes
by (71.8m points)

I have not tested this in-depth, but so far it worked for me:

mongoengine.connect('yourdb', alias='default')


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to JiKe DevOps Community for programmer and developer-Open, Learning and Share
...