简体   繁体   中英

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.

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 .

mongoengine does not work with Django-admin due to the inner-joins carried out by the admin module. You can try using a non relation version of Django like django-non-rel. However, Django-non-rel is no longer aligned with the latest version of Django.

Take a look at this question . django is new package supports the connection of admin module to MongoDB in addition to letting you have Embedded documents inside you Django models.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM