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

Description

Steps to reproduce

  1. Use django-admin to start a new project. django-admin startproject bugtest
  2. In bugtest/settings.py rename the 'default' entry in DATABASES to 'foobar'
    1. (also add a dummy 'default' entry which is an empty dict)
DATABASES = {
    'default': {},
    'foobar': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}
  1. Run migrations on foobar. ./manage.py 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. ./manage.py 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/exception.py", line 39, in inner
    response = get_response(request)
  File "/home/jibodeah/.virtualenvs/test/lib/python3.5/site-packages/django/core/handlers/base.py", 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/base.py", 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/options.py", 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/decorators.py", line 149, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "/home/jibodeah/.virtualenvs/test/lib/python3.5/site-packages/django/views/decorators/cache.py", 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/sites.py", line 211, in inner
    return view(request, *args, **kwargs)
  File "/home/jibodeah/.virtualenvs/test/lib/python3.5/site-packages/django/contrib/admin/options.py", 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/decorators.py", line 67, in _wrapper
    return bound_func(*args, **kwargs)
  File "/home/jibodeah/.virtualenvs/test/lib/python3.5/site-packages/django/utils/decorators.py", line 149, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "/home/jibodeah/.virtualenvs/test/lib/python3.5/site-packages/django/utils/decorators.py", line 63, in bound_func
    return func.__get__(self, type(self))(*args2, **kwargs2)
  File "/usr/lib64/python3.5/contextlib.py", line 29, in inner
    with self._recreate_cm():
  File "/home/jibodeah/.virtualenvs/test/lib/python3.5/site-packages/django/db/transaction.py", line 158, in __enter__
    if not connection.get_autocommit():
  File "/home/jibodeah/.virtualenvs/test/lib/python3.5/site-packages/django/db/backends/base/base.py", line 365, in get_autocommit
    self.ensure_connection()
  File "/home/jibodeah/.virtualenvs/test/lib/python3.5/site-packages/django/db/backends/dummy/base.py", 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