Mongoengine: ConnectionError: You have not defined a default connection
Asked Answered
D

4

15

In my new Django project I set up a MongoDB database and use mongoengine module but I can't properly access the dabase neither in shell nor 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')
Dry answered 17/2, 2014 at 0:8 Comment(0)
S
0

To use django with MongoDB do not use the django package available on https://www.djangoproject.com and install other packages like mongoengine, if follow this process you will find lot of difficulties.

Rather you need to use the no@rel version of django that has been forked from djangoproject and added MongoDB support and I am sure it will make setup process and development process lot easier.

Follow this link to install and set up the django with MongoDB. http://django-mongodb-engine.readthedocs.org/en/latest/topics/setup.html

One more thing you may find the error below, while setting up django.

"*Error on Django-nonrel and MongoDB: AutoField (default primary key) values must be strings representing an ObjectId on MongoDB (got u'1' instead). Please make sure your SITE_ID contains a valid ObjectId string.*"

Follow this link to fix.

https://gist.github.com/ielshareef/2986459

Please let me know if you need any more help on this.

Septi answered 17/2, 2014 at 7:37 Comment(3)
Thanks for down voting, hope you have the better solution.Septi
I already done that before including recovering the real ID site. But I'm still stuck into the same problem. Anyway, thanks for trying to help meDry
You do not need django-nonrel to use MongoDB, mongoengine has django support.Irremovable
L
21

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

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

Lovesick answered 31/7, 2014 at 8:38 Comment(0)
W
7

In your settings.py file replace:

mongoengine.connect(_MONGODB_NAME, 'localhost:27017')

with the below code (notice the added 'host='):

mongoengine.connect(_MONGODB_NAME, host='localhost:27017')
Wraf answered 2/6, 2018 at 14:55 Comment(0)
S
0

To use django with MongoDB do not use the django package available on https://www.djangoproject.com and install other packages like mongoengine, if follow this process you will find lot of difficulties.

Rather you need to use the no@rel version of django that has been forked from djangoproject and added MongoDB support and I am sure it will make setup process and development process lot easier.

Follow this link to install and set up the django with MongoDB. http://django-mongodb-engine.readthedocs.org/en/latest/topics/setup.html

One more thing you may find the error below, while setting up django.

"*Error on Django-nonrel and MongoDB: AutoField (default primary key) values must be strings representing an ObjectId on MongoDB (got u'1' instead). Please make sure your SITE_ID contains a valid ObjectId string.*"

Follow this link to fix.

https://gist.github.com/ielshareef/2986459

Please let me know if you need any more help on this.

Septi answered 17/2, 2014 at 7:37 Comment(3)
Thanks for down voting, hope you have the better solution.Septi
I already done that before including recovering the real ID site. But I'm still stuck into the same problem. Anyway, thanks for trying to help meDry
You do not need django-nonrel to use MongoDB, mongoengine has django support.Irremovable
B
0

You need to specify the mongodb database name and not _MONGODB_NAME. You also need to comment the DATABASE settings in the django.

#Database connection 
class ConnectionError(Exception):
    pass

from mongoengine import connect
try:
    connected = connect("blogDatabase", "localhost")
    if connected:
        print("Mongoengine is connected")
except ConnectionError:
    raise ConnectionError
Bergman answered 5/4, 2023 at 7:23 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.