diff options
| author | Caio Ariede <caio.ariede@gmail.com> | 2019-11-18 09:35:31 -0300 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2019-11-20 11:51:59 +0100 |
| commit | 11e42001d9b4d91ad1f7161b7242ddff5457f56a (patch) | |
| tree | 4110ffd2e0fea1bccc717d64d8732f484f19cc5e | |
| parent | 4082f078bce6ef937f604cd6270676700607b20f (diff) | |
[2.2.x] Fixed #27164 -- Fixed an example of using routers in multiple databases docs.
Make sure that AuthRouter includes ContentType in the same database.
Backport of 608e06023e6eaf75f744134a0fd203853260e616 from master
| -rw-r--r-- | docs/topics/db/multi-db.txt | 31 |
1 files changed, 19 insertions, 12 deletions
diff --git a/docs/topics/db/multi-db.txt b/docs/topics/db/multi-db.txt index 095e228949..513d11b0dd 100644 --- a/docs/topics/db/multi-db.txt +++ b/docs/topics/db/multi-db.txt @@ -301,44 +301,51 @@ databases:: } Now we'll need to handle routing. First we want a router that knows to -send queries for the ``auth`` app to ``auth_db``:: +send queries for the ``auth`` and ``contenttypes`` apps to ``auth_db`` +(``auth`` models are linked to ``ContentType``, so they must be stored in the +same database):: class AuthRouter: """ A router to control all database operations on models in the - auth application. + auth and contenttypes applications. """ + route_app_labels = {'auth', 'contenttypes'} + def db_for_read(self, model, **hints): """ - Attempts to read auth models go to auth_db. + Attempts to read auth and contenttypes models go to auth_db. """ - if model._meta.app_label == 'auth': + if model._meta.app_label in self.route_app_labels: return 'auth_db' return None def db_for_write(self, model, **hints): """ - Attempts to write auth models go to auth_db. + Attempts to write auth and contenttypes models go to auth_db. """ - if model._meta.app_label == 'auth': + if model._meta.app_label in self.route_app_labels: return 'auth_db' return None def allow_relation(self, obj1, obj2, **hints): """ - Allow relations if a model in the auth app is involved. + Allow relations if a model in the auth or contenttypes apps is + involved. """ - if obj1._meta.app_label == 'auth' or \ - obj2._meta.app_label == 'auth': + if ( + obj1._meta.app_label in self.route_app_labels or + obj2._meta.app_label in self.route_app_labels + ): return True return None def allow_migrate(self, db, app_label, model_name=None, **hints): """ - Make sure the auth app only appears in the 'auth_db' - database. + Make sure the auth and contenttypes apps only appear in the + 'auth_db' database. """ - if app_label == 'auth': + if app_label in self.route_app_labels: return db == 'auth_db' return None |
