| 219 | |
| 220 | def _router_func(action): |
| 221 | def _route_db(self, model, **hints): |
| 222 | chosen_db = None |
| 223 | for router in self.routers: |
| 224 | try: |
| 225 | method = getattr(router, action) |
| 226 | except AttributeError: |
| 227 | # If the router doesn't have a method, skip to the next |
| 228 | # one. |
| 229 | pass |
| 230 | else: |
| 231 | chosen_db = method(model, **hints) |
| 232 | if chosen_db: |
| 233 | return chosen_db |
| 234 | instance = hints.get("instance") |
| 235 | if instance is not None and instance._state.db: |
| 236 | return instance._state.db |
| 237 | return DEFAULT_DB_ALIAS |
| 238 | |
| 239 | return _route_db |
| 240 | |