pythondjangomongodbdjango-admindjango-nonrel

Django admin site cannot connect to the database - using the wrong mongodb engine


I've had the admin site working for my django app before but now I just can't find out what is the problem.

On settings.py I have the mongodb database configuration:

DATABASES = {
    'default': {
        'ENGINE': 'django_mongodb_engine', 
        'NAME': 'myapp', 
        'USER': 'username',
        'PASSWORD': '********'
        'HOST': 'myserver.com',          
        'PORT': '27157',
        'SUPPORTS_TRANSACTIONS': False,
    }
}

I know those settings are correct because I have already some views reading information from the database and displaying it. So I know for a fact that my application can connect to the mongodb database.

I've configured urls.py, admin.py and settings.py to enable the admin site (and I had it working at some point before). However now when I try to access it I just get:

Traceback:
File "/usr/local/lib/python2.6/dist-packages/django/core/handlers/base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python2.6/dist-packages/django/contrib/admin/sites.py" in wrapper
  214.                 return self.admin_view(view, cacheable)(*args, **kwargs)
File "/usr/local/lib/python2.6/dist-packages/django/utils/decorators.py" in _wrapped_view
  93.                     response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python2.6/dist-packages/django/views/decorators/cache.py" in _wrapped_view_func
  79.         response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python2.6/dist-packages/django/contrib/admin/sites.py" in inner
  195.             if not self.has_permission(request):
File "/usr/local/lib/python2.6/dist-packages/django/contrib/admin/sites.py" in has_permission
  148.         return request.user.is_active and request.user.is_staff
File "/usr/local/lib/python2.6/dist-packages/django/contrib/auth/middleware.py" in __get__
  9.             request._cached_user = get_user(request)
File "/usr/local/lib/python2.6/dist-packages/django/contrib/auth/__init__.py" in get_user
  107.         user_id = request.session[SESSION_KEY]
File "/usr/local/lib/python2.6/dist-packages/django/contrib/sessions/backends/base.py" in __getitem__
  47.         return self._session[key]
File "/usr/local/lib/python2.6/dist-packages/django/contrib/sessions/backends/base.py" in _get_session
  195.                 self._session_cache = self.load()
File "/usr/local/lib/python2.6/dist-packages/mongoengine/django/sessions.py" in load
  26.             s = MongoSession.objects(session_key=self.session_key,
File "/usr/local/lib/python2.6/dist-packages/mongoengine/queryset.py" in __get__
  1151.         db = _get_db()
File "/usr/local/lib/python2.6/dist-packages/mongoengine/connection.py" in _get_db
  41.         _connection[identity] = _get_connection(reconnect=reconnect)
File "/usr/local/lib/python2.6/dist-packages/mongoengine/connection.py" in _get_connection
  33.             raise ConnectionError('Cannot connect to the database')

Exception Type: ConnectionError at /admin/
Exception Value: Cannot connect to the database

I've introduced some debug information in the mongoengine/connection.py and I can see that the connection settings that are used to connect to the database are:

{'host': 'localhost', 'port': 27017}

which means that it is using the default connection settings instead of the settings provided by me on settings.py. Any idea of what could be causing this? Why aren't the database settings being passed to the admin site? Maybe I forgot to do something in the admin configuration?

EDIT:
I have followed these instructions to configure my django app to work with the mongodb database. I am using djangotoolbox and django-nonrel.

Just now I realized that the mongodb engine that should be used when I set 'ENGINE': 'django_mongodb_engine' is the django-mongodb-engine and not mongoengine. But in the stacktrace I can see that it is using mongoengine. (django-mongodb-engine does not depend on mongoengine) I am guessing that the right one is being used in the views (and that's why it works) but the admin is using mongoengine for some reason?!?! If understand correctly my app should not even require mongoengine to work. It is only installed because I was experimenting with it some time before.


Solution

  • I have found the problem and I'm just posting it here to close the question.

    I had the line SESSION_ENGINE = 'mongoengine.django.sessions' somewhere in my settings.py (as a result of the evil copy/past) and that was making my application to use mongoengine instead of django-mongodb-engine.