Opened 8 years ago

Closed 8 years ago

#27084 closed Bug (duplicate)

Admin site does not listen to database routers when adding objects

Reported by: Jibodeah Owned by: nobody
Component: contrib.admin Version: 1.10
Severity: Normal Keywords: admin, database, router
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


Steps to reproduce

  1. Use django-admin to start a new project. django-admin startproject bugtest
  2. In bugtest/ rename the 'default' entry in DATABASES to 'foobar'
    1. (also add a dummy 'default' entry which is an empty dict)
    'default': {},
    'foobar': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
  1. Run migrations on foobar. ./ migrate --database foobar
  2. Add a router that unconditionally maps read and write to foobar.
import threading

class Router(object):
    def db_for_read(self, model, **hints):
        database = 'foobar'
        print("Told thread {} to use {}".format(threading.get_ident(), database))
        return database
    def db_for_write(self, model, **hints):
        return self.db_for_read(model, **hints)
  1. Create a superuser on foobar. ./ createsuperuse --database foobar
  2. Run the server and navigate to /admin, login with the superuser.
  3. Click on any 'add' link to be taken to any page to add a instance of a model.

Expected Results

The page for adding an instance of a model shows up and can be used to add models to the foobar database.

Actual Results

The following error is raised:

Traceback (most recent call last):
  File "/home/jibodeah/.virtualenvs/test/lib/python3.5/site-packages/django/core/handlers/", line 39, in inner
    response = get_response(request)
  File "/home/jibodeah/.virtualenvs/test/lib/python3.5/site-packages/django/core/handlers/", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/jibodeah/.virtualenvs/test/lib/python3.5/site-packages/django/core/handlers/", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/jibodeah/.virtualenvs/test/lib/python3.5/site-packages/django/contrib/admin/", line 544, in wrapper
    return self.admin_site.admin_view(view)(*args, **kwargs)
  File "/home/jibodeah/.virtualenvs/test/lib/python3.5/site-packages/django/utils/", line 149, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "/home/jibodeah/.virtualenvs/test/lib/python3.5/site-packages/django/views/decorators/", line 57, in _wrapped_view_func
    response = view_func(request, *args, **kwargs)
  File "/home/jibodeah/.virtualenvs/test/lib/python3.5/site-packages/django/contrib/admin/", line 211, in inner
    return view(request, *args, **kwargs)
  File "/home/jibodeah/.virtualenvs/test/lib/python3.5/site-packages/django/contrib/admin/", line 1509, in add_view
    return self.changeform_view(request, None, form_url, extra_context)
  File "/home/jibodeah/.virtualenvs/test/lib/python3.5/site-packages/django/utils/", line 67, in _wrapper
    return bound_func(*args, **kwargs)
  File "/home/jibodeah/.virtualenvs/test/lib/python3.5/site-packages/django/utils/", line 149, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "/home/jibodeah/.virtualenvs/test/lib/python3.5/site-packages/django/utils/", line 63, in bound_func
    return func.__get__(self, type(self))(*args2, **kwargs2)
  File "/usr/lib64/python3.5/", line 29, in inner
    with self._recreate_cm():
  File "/home/jibodeah/.virtualenvs/test/lib/python3.5/site-packages/django/db/", line 158, in __enter__
    if not connection.get_autocommit():
  File "/home/jibodeah/.virtualenvs/test/lib/python3.5/site-packages/django/db/backends/base/", line 365, in get_autocommit
  File "/home/jibodeah/.virtualenvs/test/lib/python3.5/site-packages/django/db/backends/dummy/", line 21, in complain
    raise ImproperlyConfigured("settings.DATABASES is improperly configured. "
django.core.exceptions.ImproperlyConfigured: settings.DATABASES is improperly configured. Please supply the ENGINE value. Check settings documentation for more details.

Implying that the admin site is trying to use the default database, ignoring the router's suggestion.

Change History (1)

comment:1 by Tim Graham, 8 years ago

Resolution: duplicate
Status: newclosed

Probably a duplicate of #26170.

Note: See TracTickets for help on using tickets.
Back to Top