oza*_*zan 5 python django django-models django-admin
我正在使用两个数据库,一个 Mysql 另一个是 PostgreSql。
Mysql 定义为 'default',PostgreSql 定义为 'location_db'
我有 5 个应用程序,比如说“a、b、c、d、位置”应用程序,
除位置应用程序外,所有应用程序都应在“默认”数据库上运行所有内容,
我只是做 ' python manage.py runserver ' 然后一切正常,甚至站点也可以正常工作,但是当我在管理页面然后单击 'location' 管理模型时,它运行错误* 关系“location_locationmodel”不存在;第 1 行:SELECT COUNT(*) AS "__count" FROM "location_locationmodel" *另一方面,如果我单击“abcd”管理模型,它就可以正常工作。
1)这是 router.py(我只是从 doc.django 站点获取路由器代码,也许这些代码不适合我,因为我只是猜测我的路由器配置不正确?也许?)
class LocationRouter(object):
"""
A router to control all database operations on models in the
auth application.
"""
def db_for_read(self, model, **hints):
"""
Attempts to read auth models go to location_db.
"""
if model._meta.app_label == 'location':
return 'location_db'
return None
def db_for_write(self, model, **hints):
"""
Attempts to write auth models go to location_db.
"""
if model._meta.app_label == 'location':
return 'location_db'
return None
def allow_relation(self, obj1, obj2, **hints):
"""
Allow relations if a model in the events app is involved.
"""
if obj1._meta.app_label == 'location' or \
obj2._meta.app_label == 'location':
return True
return None
def allow_migrate(self, db, app_label, model_name=None, **hints):
"""
Make sure the auth app only appears in the 'location_db'
database.
"""
if app_label == 'location':
return db == 'location_db'
return None
Run Code Online (Sandbox Code Playgroud)
2)这是settings.py
DATABASE_ROUTERS = ['location.router.LocationRouter']
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
#'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
'OPTIONS': {
'read_default_file': os.path.join(BASE_DIR, 'my.cnf'),
},
},
'location_db': {
'ENGINE': 'django.contrib.gis.db.backends.postgis',
'NAME': 'geo',
'USER': 'john',
'PASSWORD': 'toor',
'HOST': 'localhost',
'PORT': '5432',
},
}
Run Code Online (Sandbox Code Playgroud)
* 我的预测 *
1)如果我创建另一个项目并且只使用 PostgreSql 那么一切正常,所以所有数据库都可以正常工作
2) 这是我执行“python manage.py makemigrations 或 migrate”时:
Operations to perform:
Apply all migrations: admin, auth, a, contenttypes, location, b, c, d, registration, sessions, sites
Running migrations:
No migrations to apply.
Run Code Online (Sandbox Code Playgroud)
* 迁移的结果我认为不应该带有“位置”应用程序,或者可能会出现 . 这只是我不知道的预测,但是当我在管理页面上单击“位置”管理模型时,它会运行错误,但当我只使用一个数据库时它不会运行错误,我的意思是它搜索仍在 mysql 数据库中的表吗?可能是路由器配置不正确?*
好吧,如果您没有失眠,那么解决方案并不算太远,在这种情况下,您只需指定所有内容即可
就是这样,你的查询在 postgresql 数据库中匹配:)祝你好运
| 归档时间: |
|
| 查看次数: |
6998 次 |
| 最近记录: |