我有一个在 sqlite3
数据库上运行的原始 Django 项目。我想集成另一个运行在 postgre
数据库上的 GitHub 的 Django 项目。我在 requirements.txt 文件中添加了 github 链接并安装了它,并在 settings.py 文件的原始 INSTALLED_APPS 部分添加了新的 Django 项目应用程序,还更新了原始项目的 urls.py 文件。现在我遇到了如何合并这两个项目的 settings.py 的问题?
当我运行命令时
python manage.py 迁移
然后它给了我这个错误:-
AttributeError: 'DatabaseOperations' 对象没有属性 'geo_db_type'
正确的设置已经在github下载的django项目中,但在原始项目中没有。
我几乎阅读了 Stack Overflow 和官方文档中的所有答案。但是能够理解。 如果这是一个重复或愚蠢的问题,我深表歉意。
最佳答案
根据文档,您需要通过以下方式编辑您的设置: 1) 添加一个新文件调用 ex databases.py
DATABASES = {
'default': {},
'auth_db': {
'NAME': 'auth_db',
'ENGINE': 'django.db.backends.mysql',
'USER': 'mysql_user',
'PASSWORD': 'swordfish',
},
'primary': {
'NAME': 'primary',
'ENGINE': 'django.db.backends.mysql',
'USER': 'mysql_user',
'PASSWORD': 'spam',
},
'replica1': {
'NAME': 'replica1',
'ENGINE': 'django.db.backends.mysql',
'USER': 'mysql_user',
'PASSWORD': 'eggs',
},
'replica2': {
'NAME': 'replica2',
'ENGINE': 'django.db.backends.mysql',
'USER': 'mysql_user',
'PASSWORD': 'bacon',
},
2) 对于 authrouter 文件,请参阅文档,这里是一个示例:
import random
class PrimaryReplicaRouter(object):
def db_for_read(self, model, **hints):
"""
Reads go to a randomly-chosen replica.
"""
return random.choice(['replica1', 'replica2'])
def db_for_write(self, model, **hints):
"""
Writes always go to primary.
"""
return 'primary'
def allow_relation(self, obj1, obj2, **hints):
"""
Relations between objects are allowed if both objects are
in the primary/replica pool.
"""
db_list = ('primary', 'replica1', 'replica2')
if obj1._state.db in db_list and obj2._state.db in db_list:
return True
return None
def allow_migrate(self, db, app_label, model_name=None, **hints):
"""
All non-auth models end up in this pool.
"""
return Trueta.app_label == 'auth':
return 'auth_db'
return None
def allow_relation(self, obj1, obj2, **hints):
"""
Allow relations if a model in the auth app is involved.
"""
if obj1._meta.app_label == 'auth' or \
obj2._meta.app_label == 'auth':
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.
"""
if app_label == 'auth':
return db == 'auth_db'
return None
3) 在你的 settings.py 中删除数据库模式并添加这一行
DATABASE_ROUTERS = ['yourapp.filename.CLASS']
当你午餐时使用命令“migrate”添加 --database=databasename 以应用它们 有关详细信息,请参阅 https://docs.djangoproject.com/en/1.10/topics/db/multi-db/
关于python - 如何集成两个不同数据库的Django项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43094699/