我正在尝试在我的一个 django 项目中设置 celery。我希望 celery 使用单独的数据库。目前,由于项目处于开发阶段,我们正在使用 sqlite3。为了设置多个数据库,我做了以下操作。
在 settings.py 文件中定义的数据库。
DATABASES = {'default':
{'ENGINE': 'django.db.backends.sqlite3',
'NAME':'devel',
'USER':'',
'PASSWORD':'',
'HOST':'',
'PORT':'',
},
'celery':
{'ENGINE': 'django.db.backends.sqlite3',
'NAME':'celery',
'USER':'',
'PASSWORD':'',
'HOST':'',
'PORT':'',
},
}
在 db_routers.py 文件中创建了一个路由器对象
class CeleryRouter(object):
"""
This class will route all celery related models to a»
separate database.
"""
# Define the applications to be used in the celery database
APPS = (
'django',
'djcelery'
)
# Define Database Alias
DB = 'celery'
def db_for_read(self, model, **hints):
"""
Point read operations to celery database.
"""
if model._meta.app_label in self.APPS:
return self.DB
return None
def db_for_write(self, model, **hints):
"""
Point write operations to celery database.
"""
if model._meta.app_label in self.APPS:
return self.DB
return None
def allow_relation(self, obj1, obj2, **hints):
"""
Allow any relation between two objects in the db pool
"""
if (obj1._meta.app_label is self.APPS) and \
(obj2._meta.app_label in self.APPS):
return True
return None
def allow_syncdb(self, db, model):
"""
Make sure the celery tables appear only in celery
database.
"""
if db == self.DB:
return model._meta.app_label in self.APPS
elif model._meta.app_label in self.APPS:
return False
return None
更新了DATABASE_ROUTER
settings.py 文件中的变量
DATABASE_ROUTERS = [
'appname.db_routers.CeleryRouter',
]
现在,当我python manage.py syncdb
看到这些表是为 celery 创建的,但只创建了一个数据库,即devel
。为什么在devel
数据库中而不是在celery
数据库中创建表?