Search code examples
mysqldjangodjango-rest-frameworkdjongo

django connect to mysql and mongodb


I have 2 databases mysql and mongo. On mysql people hold all data and on mongo just images. I have defined them like this

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': '*****',
        'USER': '*****',
        'PASSWORD': '*****',
        'HOST': '1*****',
        'PORT': '1*****',
        'TEST': {
            'NAME': '*****',
            # 'CHARSET': 'utf8',
        },
        'mongodb':  {
            'ENGINE': 'djongo',
            'NAME': '*****',
            'USER': '*****',
            'PASSWORD': '*****',
            'HOST': '*****',
            'PORT': '*****',
            'TEST': {
                'NAME': '*****',
                # 'CHARSET': 'utf8',
            },
        }

    }
}

Now I want to run inspectdb on mongodb and I het this error

manage.py inspectdb --database mongodb
django.db.utils.ConnectionDoesNotExist: The connection mongodb doesn't exist

The host password port is ok


Solution

  • You wrote mongodb at the same level as 'TEST', etc. This thus means that you only defined one database, and that mongodb is a key of that dictionary.

    You thus should specify the mongodb database at the same level like the default database:

    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'NAME': '*****',
            'USER': '*****',
            'PASSWORD': '*****',
            'HOST': '1*****',
            'PORT': '1*****',
            'TEST': {
                'NAME': '*****',
                # 'CHARSET': 'utf8',
            }
        },
        'mongodb':  {
            'ENGINE': 'djongo',
            'NAME': '*****',
            'USER': '*****',
            'PASSWORD': '*****',
            'HOST': '*****',
            'PORT': '*****',
            'TEST': {
                'NAME': '*****',
                # 'CHARSET': 'utf8',
            },
        }
    }