#27084: Admin site does not listen to database routers when adding objects
-------------------------------+-------------------------------------
     Reporter:  Jibodeah       |      Owner:  nobody
         Type:  Bug            |     Status:  new
    Component:  contrib.admin  |    Version:  1.10
     Severity:  Normal         |   Keywords:  admin, database, router
 Triage Stage:  Unreviewed     |  Has patch:  0
Easy pickings:  0              |      UI/UX:  0
-------------------------------+-------------------------------------
 == 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'
     a. (also add a dummy 'default' entry which is an empty dict)


 {{{
 #!python
 DATABASES = {
     'default': {},
     'foobar': {
         'ENGINE': 'django.db.backends.sqlite3',
         'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
     }
 }
 }}}


 3. Run migrations on foobar. `./manage.py migrate --database foobar`
 4. Add a router that unconditionally maps read and write to foobar.


 {{{
 #!python
 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)
 }}}


 5. Create a superuser on foobar. `./manage.py createsuperuse --database
 foobar`
 6. Run the server and navigate to /admin, login with the superuser.
 7. 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.

--
Ticket URL: <https://code.djangoproject.com/ticket/27084>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/051.8d7ce8359059797ac8bfb61b8852fc4d%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to