Opened 5 years ago

Closed 5 years ago

Last modified 5 years ago

#25564 closed Bug (duplicate)

Django Admin's "View on Site" ignores model's custom manager

Reported by: Yurii Zolot'ko Owned by: nobody
Component: contrib.contenttypes Version: 1.8
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

When there is model A located in DB other than default having custom manager and/or custom DB router to query appropriate database they both are ignored by django admin's "View on Site" feature. The problem indeed is deeper in content types get_object_for_this_type() implementation. Example source code:

class Manager(models.Manager):

    def get_queryset(self):
        return super(Manager, self).get_queryset().using('other')

class A(models.Model):
    ...
    objects = Manager()

class Router(object):

    def db_for_read(self, model, **hints):
        if isinstance(model, A):
            return 'other'

    def db_for_write(self, model, **hints):
        if isinstance(model, A):
            return 'other'

Current behaviour: with this setup clicking "View on Site" at model A's admin still results at "no such table: a" with traceback:

Request Method: GET
Request URL: http://localhost:8000/admin/r/27/1432/

Traceback:
File "/home/i/frontend/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  132.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/i/frontend/local/lib/python2.7/site-packages/django/contrib/admin/sites.py" in wrapper
  254.                 return self.admin_view(view, cacheable)(*args, **kwargs)
File "/home/i/frontend/local/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapped_view
  110.                     response = view_func(request, *args, **kwargs)
File "/home/i/frontend/local/lib/python2.7/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
  57.         response = view_func(request, *args, **kwargs)
File "/home/i/frontend/local/lib/python2.7/site-packages/django/contrib/admin/sites.py" in inner
  233.             return view(request, *args, **kwargs)
File "/home/i/frontend/local/lib/python2.7/site-packages/django/contrib/contenttypes/views.py" in shortcut
  21.         obj = content_type.get_object_for_this_type(pk=object_id)
File "/home/i/frontend/local/lib/python2.7/site-packages/django/contrib/contenttypes/models.py" in get_object_for_this_type
  194.         return self.model_class()._base_manager.using(self._state.db).get(**kwargs)
File "/home/i/frontend/local/lib/python2.7/site-packages/django/db/models/query.py" in get
  328.         num = len(clone)
File "/home/i/frontend/local/lib/python2.7/site-packages/django/db/models/query.py" in __len__
  144.         self._fetch_all()
File "/home/i/frontend/local/lib/python2.7/site-packages/django/db/models/query.py" in _fetch_all
  965.             self._result_cache = list(self.iterator())
File "/home/i/frontend/local/lib/python2.7/site-packages/django/db/models/query.py" in iterator
  238.         results = compiler.execute_sql()
File "/home/i/frontend/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py" in execute_sql
  840.             cursor.execute(sql, params)
File "/home/i/frontend/local/lib/python2.7/site-packages/debug_toolbar/panels/sql/tracking.py" in execute
  159.         return self._record(self.cursor.execute, sql, params)
File "/home/i/frontend/local/lib/python2.7/site-packages/debug_toolbar/panels/sql/tracking.py" in _record
  101.             return method(sql, params)
File "/home/i/frontend/local/lib/python2.7/site-packages/django/db/backends/utils.py" in execute
  79.             return super(CursorDebugWrapper, self).execute(sql, params)
File "/home/i/frontend/local/lib/python2.7/site-packages/django/db/backends/utils.py" in execute
  64.                 return self.cursor.execute(sql, params)
File "/home/i/frontend/local/lib/python2.7/site-packages/django/db/utils.py" in __exit__
  97.                 six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/home/i/frontend/local/lib/python2.7/site-packages/django/db/backends/utils.py" in execute
  64.                 return self.cursor.execute(sql, params)
File "/home/i/frontend/local/lib/python2.7/site-packages/django/db/backends/sqlite3/base.py" in execute
  318.         return Database.Cursor.execute(self, query, params)

Exception Type: OperationalError at /admin/r/27/1432/
Exception Value: no such table: categories

Expected behaviour: django admin and get_object_for_this_type() should use custom router / custom manager

Change History (3)

comment:1 Changed 5 years ago by Tim Graham

Looks like a duplicate of #16281. Can you confirm?

comment:2 Changed 5 years ago by Tim Graham

Component: Uncategorizedcontrib.contenttypes
Resolution: duplicate
Status: newclosed
Type: UncategorizedBug

comment:3 Changed 5 years ago by Yurii Zolot'ko

I confirm, looks related.

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