1
0
mirror of https://github.com/django/django.git synced 2024-12-25 10:35:48 +00:00

Fixed #14870 -- don't catch AttributeErrors in database router methods, instead check that the method itself exists. Thanks to jonash for the patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@14857 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Alex Gaynor 2010-12-08 18:31:57 +00:00
parent b8c368feb2
commit 2a46323124

View File

@ -5,6 +5,7 @@ from django.conf import settings
from django.core.exceptions import ImproperlyConfigured from django.core.exceptions import ImproperlyConfigured
from django.utils.importlib import import_module from django.utils.importlib import import_module
DEFAULT_DB_ALIAS = 'default' DEFAULT_DB_ALIAS = 'default'
# Define some exceptions that mirror the PEP249 interface. # Define some exceptions that mirror the PEP249 interface.
@ -125,12 +126,15 @@ class ConnectionRouter(object):
chosen_db = None chosen_db = None
for router in self.routers: for router in self.routers:
try: try:
chosen_db = getattr(router, action)(model, **hints) method = getattr(router, action)
if chosen_db:
return chosen_db
except AttributeError: except AttributeError:
# If the router doesn't have a method, skip to the next one. # If the router doesn't have a method, skip to the next one.
pass pass
else:
chosen_db = method(model, **hints
)
if chosen_db:
return chosen_db
try: try:
return hints['instance']._state.db or DEFAULT_DB_ALIAS return hints['instance']._state.db or DEFAULT_DB_ALIAS
except KeyError: except KeyError:
@ -143,21 +147,25 @@ class ConnectionRouter(object):
def allow_relation(self, obj1, obj2, **hints): def allow_relation(self, obj1, obj2, **hints):
for router in self.routers: for router in self.routers:
try: try:
allow = router.allow_relation(obj1, obj2, **hints) method = router.allow_relation
if allow is not None:
return allow
except AttributeError: except AttributeError:
# If the router doesn't have a method, skip to the next one. # If the router doesn't have a method, skip to the next one.
pass pass
else:
allow = method(obj1, obj2, **hints)
if allow is not None:
return allow
return obj1._state.db == obj2._state.db return obj1._state.db == obj2._state.db
def allow_syncdb(self, db, model): def allow_syncdb(self, db, model):
for router in self.routers: for router in self.routers:
try: try:
allow = router.allow_syncdb(db, model) method = router.allow_syncdb
if allow is not None:
return allow
except AttributeError: except AttributeError:
# If the router doesn't have a method, skip to the next one. # If the router doesn't have a method, skip to the next one.
pass pass
else:
allow = method(db, model)
if allow is not None:
return allow
return True return True