python - Django Multiple DB, authenticating from NON default db -
i have 2 databases
configured in django project:
default
reports
the reports
database contains auth_user
, while authenticating user, users checked in default
database. how can use authenticate()
reports
database?
you can custom database router, shown in the django documentation covers kind of situation:
class authrouter(object): """ router control database operations on models in auth application. """ def db_for_read(self, model, **hints): """ attempts read auth models go reports. """ if model._meta.app_label == 'auth': return 'reports' return none def db_for_write(self, model, **hints): """ attempts write auth models go reports. """ if model._meta.app_label == 'auth': return 'reports' return none def allow_relation(self, obj1, obj2, **hints): """ allow relations if model in auth app involved. """ if obj1._meta.app_label == 'auth' or \ obj2._meta.app_label == 'auth': return true return none def allow_syncdb(self, db, model): """ make sure auth app appears in 'reports' database. """ if db == 'reports': return model._meta.app_label == 'auth' elif model._meta.app_label == 'auth': return false return none
and you'll need put dotted-path class database_routers
in settings.py
.
there some notes using django contrib apps multiple databases worth reading. in particular, auth
tables need contenttype
backing table in same database.
it may easier make reports
default, , use 1 you're calling default
explicitly, needed.
Comments
Post a Comment