在 Django 项目中,我有一个应用程序 otherapp,它访问远程服务器上的 Postgres 数据库,其中包含抓取的数据。我有第二个应用程序内容,它访问同一远程服务器上的不同Postgres 数据库,并包含我希望通过 Wagtail CMS 提供服务的页面。
我使用这些说明在本地安装了 Wagtail(我没有使用 Wagtail 安装程序)。我让它在本地工作。然后,我做了一个本地数据库的 pg_dump 并在远程数据库服务器上做了 psql db2 < db2dumpfile.sql。
每个应用程序都可以单独在本地正常工作,但我无法让它们一起工作。我想我可以使用数据库路由器来指定我想使用哪个数据库来检索不同类型的数据。
但是,当我将数据库路由器放入设置文件时,它开始出现故障。我怎样才能解决这个问题?我需要在项目的其他地方声明 wagtailcore 吗?
设置.py:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'db1',
'USER': DB_USERNAME,
'PASSWORD': DB_PASSWORD,
'HOST': HOST,
'PORT': PORT
},
'CMS': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'db2',
'USER': DB_USERNAME,
'PASSWORD': DB_PASSWORD,
'HOST': HOST,
'PORT': PORT
}
}
DATABASE_ROUTERS = [ 'projectname.routers.FindRouter',]
路由器.py:
import os
from django.conf import settings
import socket
class FindRouter(object):
def db_for_read(self, model, **hints) :
if model._meta.app_label == 'content' :
return 'CMS'
return None
def db_for_read(self, model, **hints):
if model._meta.app_label == 'content' :
return 'CMS'
return None
def db_for_write(self, model, **hints):
if model._meta.app_label == 'content' :
return 'CMS'
return None
def allow_relation(self, obj1, obj2, **hints):
if obj1._meta.app_label == 'content' or obj2._meta.app_label == 'content':
return True
return None
def allow_migrate(self, db, app_label, model=None, **hints):
if app_label == 'content' :
return db == 'CMS'
return None
这是我在运行服务器时遇到的错误:
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/cms/
Django Version: 1.9
Python Version: 2.7.10
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'wagtail.wagtailforms',
'wagtail.wagtailredirects',
'wagtail.wagtailembeds',
'wagtail.wagtailsites',
'wagtail.wagtailusers',
'wagtail.wagtailsnippets',
'wagtail.wagtaildocs',
'wagtail.wagtailimages',
'wagtail.wagtailsearch',
'wagtail.wagtailadmin',
'wagtail.wagtailcore',
'modelcluster',
'compressor',
'taggit',
'otherapp',
'content']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware',
'wagtail.wagtailcore.middleware.SiteMiddleware',
'wagtail.wagtailredirects.middleware.RedirectMiddleware']
Traceback:
File "/Users/username/.virtualenvs/bail/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
123. response = middleware_method(request)
File "/Users/username/.virtualenvs/bail/lib/python2.7/site-packages/wagtail/wagtailcore/middleware.py" in process_request
11. request.site = Site.find_for_request(request)
File "/Users/username/.virtualenvs/bail/lib/python2.7/site-packages/wagtail/wagtailcore/models.py" in find_for_request
122. return Site.objects.get(hostname=hostname) # Site.DoesNotExist here goes to the final except clause
File "/Users/username/.virtualenvs/bail/lib/python2.7/site-packages/django/db/models/manager.py" in manager_method
122. return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/Users/username/.virtualenvs/bail/lib/python2.7/site-packages/django/db/models/query.py" in get
381. num = len(clone)
File "/Users/username/.virtualenvs/bail/lib/python2.7/site-packages/django/db/models/query.py" in __len__
240. self._fetch_all()
File "/Users/username/.virtualenvs/bail/lib/python2.7/site-packages/django/db/models/query.py" in _fetch_all
1074. self._result_cache = list(self.iterator())
File "/Users/username/.virtualenvs/bail/lib/python2.7/site-packages/django/db/models/query.py" in __iter__
52. results = compiler.execute_sql()
File "/Users/username/.virtualenvs/bail/lib/python2.7/site-packages/django/db/models/sql/compiler.py" in execute_sql
852. cursor.execute(sql, params)
File "/Users/username/.virtualenvs/bail/lib/python2.7/site-packages/django/db/backends/utils.py" in execute
79. return super(CursorDebugWrapper, self).execute(sql, params)
File "/Users/username/.virtualenvs/bail/lib/python2.7/site-packages/django/db/backends/utils.py" in execute
64. return self.cursor.execute(sql, params)
File "/Users/username/.virtualenvs/bail/lib/python2.7/site-packages/django/db/utils.py" in __exit__
95. six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/Users/username/.virtualenvs/bail/lib/python2.7/site-packages/django/db/backends/utils.py" in execute
64. return self.cursor.execute(sql, params)
Exception Type: ProgrammingError at /cms/
Exception Value: relation "wagtailcore_site" does not exist
LINE 1: ...ge_id", "wagtailcore_site"."is_default_site" FROM "wagtailco...
^