Ticket #14870: router-hasattr.patch

File router-hasattr.patch, 2.2 KB (added by Jonas H., 14 years ago)
  • django/db/utils.py

    diff -r 803236a054d3 django/db/utils.py
    a b  
    124124        def _route_db(self, model, **hints):
    125125            chosen_db = None
    126126            for router in self.routers:
    127                 try:
    128                     chosen_db = getattr(router, action)(model, **hints)
    129                     if chosen_db:
    130                         return chosen_db
    131                 except AttributeError:
    132                     # If the router doesn't have a method, skip to the next one.
    133                     pass
     127                method = getattr(router, action, None)
     128                if method is None:
     129                    continue
     130                chosen_db = method(model, **hints)
     131                if chosen_db:
     132                    return chosen_db
    134133            try:
    135134                return hints['instance']._state.db or DEFAULT_DB_ALIAS
    136135            except KeyError:
     
    142141
    143142    def allow_relation(self, obj1, obj2, **hints):
    144143        for router in self.routers:
    145             try:
    146                 allow = router.allow_relation(obj1, obj2, **hints)
    147                 if allow is not None:
    148                     return allow
    149             except AttributeError:
    150                 # If the router doesn't have a method, skip to the next one.
    151                 pass
     144            if not hasattr(router, 'allow_relation'):
     145                continue
     146            allow = router.allow_relation(obj1, obj2, **hints)
     147            if allow is not None:
     148                return allow
    152149        return obj1._state.db == obj2._state.db
    153150
    154151    def allow_syncdb(self, db, model):
    155152        for router in self.routers:
    156             try:
    157                 allow = router.allow_syncdb(db, model)
    158                 if allow is not None:
    159                     return allow
    160             except AttributeError:
    161                 # If the router doesn't have a method, skip to the next one.
    162                 pass
     153            if not hasattr(router, 'allow_syncdb'):
     154                continue
     155            allow = router.allow_syncdb(db, model)
     156            if allow is not None:
     157                return allow
    163158        return True
Back to Top