Django - 多个数据库和 auth.Permission
我正在开发一个需要两个数据库的项目 - 一个用于“注销”部分,一个用于登录。我需要将身份验证(以及内容类型)应用程序同步到两个数据库,这工作正常。但是,创建默认 Permission 和 ContentType 对象的 auth 和 contenttypes 管理命令不在登录数据库上运行,仅在默认数据库上运行。我有这个权利吗?
我的数据库路由器
LOGGED_IN_APPS = ('avatar', 'guardian', 'money', 'ipn', 'schedule', 'studio')
COMMON_APPS = ('auth', 'contenttypes', 'registration')
class MyRouter(object):
def db_for_read(self, model, **hints):
if model._meta.app_label in LOGGED_IN_APPS:
return 'logged_in'
return None
def db_for_read(self, model, **hints):
if model._meta.app_label in LOGGED_IN_APPS:
return 'logged_in'
return None
def allow_relation(self, obj1, obj2, **hints):
if obj1._meta.app_label in LOGGED_IN_APPS or obj2._meta.app_label in LOGGED_IN_APPS:
return True
return None
def allow_syncdb(self, db, model):
if db == 'logged_in':
return model._meta.app_label in LOGGED_IN_APPS or model._meta.app_label in COMMON_APPS
elif model._meta.app_label in LOGGED_IN_APPS:
return False
return None
I'm working on a project that needs two databases - one for the "logged out" portion and one for the logged in. I need the auth (and therefore contenttypes) app synched to both databases, which is working fine. However, the management commands for auth and contenttypes that create the default Permission and ContentType objects aren't running on the logged in database, only the default one. Do I have this right?
My database router
LOGGED_IN_APPS = ('avatar', 'guardian', 'money', 'ipn', 'schedule', 'studio')
COMMON_APPS = ('auth', 'contenttypes', 'registration')
class MyRouter(object):
def db_for_read(self, model, **hints):
if model._meta.app_label in LOGGED_IN_APPS:
return 'logged_in'
return None
def db_for_read(self, model, **hints):
if model._meta.app_label in LOGGED_IN_APPS:
return 'logged_in'
return None
def allow_relation(self, obj1, obj2, **hints):
if obj1._meta.app_label in LOGGED_IN_APPS or obj2._meta.app_label in LOGGED_IN_APPS:
return True
return None
def allow_syncdb(self, db, model):
if db == 'logged_in':
return model._meta.app_label in LOGGED_IN_APPS or model._meta.app_label in COMMON_APPS
elif model._meta.app_label in LOGGED_IN_APPS:
return False
return None
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这就是我所做的。首先,无法告诉syncdb 在特定数据库上创建权限 - 它始终会选择默认值。因此,由于这个项目的性质,我能够将其分成两个项目,每个项目都有自己的数据库。这解决了我的问题,但不幸的是 Django 需要修补才能支持在多个数据库上执行此操作。
Here's what I did. First, there is no way to tell syncdb to create permissions on a specific database - it always will pick the default. So, because of the nature of this project, I was able to split it into two projects, each with their own database. This solves the problem for me, but unfortunately Django would need to be patched to support doing this on multiple databases.