gpt4 book ai didi

python - 运行 Django 的 ./manage.py syncdb 时禁用更新 content_types

转载 作者:太空宇宙 更新时间:2023-11-04 06:14:00 25 4
gpt4 key购买 nike

我的项目正在使用多个数据库来管理身份验证、内容类型信息和项目应用程序。

我的数据库设置:

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'test_default',
'USER': 'root',
'PASSWORD': 'root',
'PORT': '',
},
'auth_db': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'test_auth_db',
'USER': 'root',
'PASSWORD': 'root',
'HOST': '',
'PORT': '',
}

我有一个路由器来管理读写

def db_for_read(self, model, **hints):
if model._meta.app_label == "auth" or model._meta.app_label == "sessions" or model._meta.app_label == "contenttypes":
return "auth_db"
return None

def db_for_write(self, model, **hints):
if model._meta.app_label == "auth" or model._meta.app_label == "sessions" or model._meta.app_label == "contenttypes":
return "auth_db"
return None

def allow_syncdb(self, db, model):

return True

每当我执行 syncdb 时,所有应用程序内容类型都会在“auth_db”中更新。我想停止这个并与“默认”数据库同步。这样所有表都只会在默认数据库中创建和更新。

当项目运行时,它将根据路由器配置使用读写。

我该怎么做?

最佳答案

我通过更新写入函数修复了它

def db_for_write(self, model, **hints):
if model._meta.app_label in ("sessions",) :
return "auth_db"
return "default"

这样syncdb就不会向“auth”和“contenttypes”写入任何数据

关于python - 运行 Django 的 ./manage.py syncdb 时禁用更新 content_types,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17038951/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com