3
votes

I'm trying to connect the mongo(v4.0.3)db with django(v1.11) using mongoengine(0.6.0) driver. It is showing connection does not exist.

my settings.py file is as follows:

import os

import mongoengine

dbname = 'newdb'

mongoengine.connect(dbname, host='127.0.0.1', port = 27017)

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

SECRET_KEY = ''

DEBUG = True

ALLOWED_HOSTS = []


INSTALLED_APPS = [

    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'mdbcapp',
 ]

 MIDDLEWARE = [

    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
 ]

 ROOT_URLCONF = 'multiple_databses_connections.urls'

 TEMPLATES = [

    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
 ]

 WSGI_APPLICATION = 'multiple_databses_connections.wsgi.application'

 DATABASES = {

    'dfault': {
        'ENGINE': '',
        'NAME'  : ''
       }


 AUTH_PASSWORD_VALIDATORS = [

    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },


LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

STATIC_URL = '/static/'

I'm getting following errors:

Traceback (most recent call last):

File "manage.py", line 22, in execute_from_command_line(sys.argv) File "/usr/local/lib/python2.7/dist-packages/django/core/management/init.py", line 363, in execute_from_command_line utility.execute() File "/usr/local/lib/python2.7/dist-packages/django/core/management/init.py", line 337, in execute django.setup() File "/usr/local/lib/python2.7/dist-packages/django/init.py", line 27, in setup apps.populate(settings.INSTALLED_APPS) File "/usr/local/lib/python2.7/dist-packages/django/apps/registry.py", line 108, in populate app_config.import_models() File "/usr/local/lib/python2.7/dist-packages/django/apps/config.py", line 202, in import_models self.models_module = import_module(models_module_name) File "/usr/lib/python2.7/importlib/init.py", line 37, in import_module import(name) File "/usr/local/lib/python2.7/dist-packages/django/contrib/auth/models.py", line 4, in from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager File "/usr/local/lib/python2.7/dist-packages/django/contrib/auth/base_user.py", line 52, in class AbstractBaseUser(models.Model): File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 124, in new new_class.add_to_class('_meta', Options(meta, app_label)) File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 330, in add_to_class value.contribute_to_class(cls, name) File "/usr/local/lib/python2.7/dist-packages/django/db/models/options.py", line 214, in contribute_to_class self.db_table = truncate_name(self.db_table, connection.ops.max_name_length()) File "/usr/local/lib/python2.7/dist-packages/django/db/init.py", line 33, in getattr return getattr(connections[DEFAULT_DB_ALIAS], item) File "/usr/local/lib/python2.7/dist-packages/django/db/utils.py", line 208, in getitem self.ensure_defaults(alias) File "/usr/local/lib/python2.7/dist-packages/django/db/utils.py", line 178, in ensure_defaults raise ConnectionDoesNotExist("The connection %s doesn't exist" % alias) django.db.utils.ConnectionDoesNotExist: The connection default doesn't exist

How do I solve the problem?

2
Can you update your question with your DATABASES setting? The sensitive items can be replaced. And make sure that you can connect to your database with mongo client.Ben Lee
As you can see, DATABASES['default'] is empty. Wish this django mongodb docs can help you. PS: Indentation with 4 spaces can make part of your text look like code.Ben Lee
DATABASES = { 'default': { 'ENGINE': 'django_mongodb_engine', 'NAME' : 'newdb', 'USER': 'admin', 'PASSWORD': 'admin', 'HOST': '127.0.0.1', 'PORT': '27017', #'SUPPORTS_TRANSACTIONS': False, }Kabali
i tried above but i got theese errors .,, Try using 'django.db.backends.XXX', where XXX is one of: 'mysql', 'oracle', 'postgresql', 'sqlite3' Error was: No module named utilKabali

2 Answers

0
votes

You are using only the Mongo engine. But in the settings file, the database ENGINE key is mandatory. Add this line in ENGINE:

DATABASES = {
    'default': {
        'ENGINE': 'django.core.cache.backends.dummy.DummyCache',
        'NAME': 'mydatabase',
     }
}

But it may throw an error like database improperly configured which is ignorable.

For more information ref link https://docs.djangoproject.com/en/dev/ref/settings/#backend

0
votes

Default settings for Sqlite

DATABASES = {
'default': {
    'ENGINE': 'django.db.backends.sqlite3',
    'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
 }
}

Settings for MySQL

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