Django Celery和多个数据库(Celery,Django和RabbitMQ) [英] Django Celery and multiple databases (Celery, Django and RabbitMQ)
问题描述
我可以设置一个与Django Celery一起使用的不同数据库吗?我有一个项目配置有多个数据库,不希望Django Celery使用默认值。
如果我仍然可以使用django芹菜管理页面并读取存储在这个不同数据库中的结果,我将会很好:)
应该可以使用Django数据库路由器为django-celery模型设置一个单独的数据库:
https:// docs。 djangoproject.com/en/1.4/topics/db/multi-db/#automatic-database-routing
我没有使用django进行测试-cery,但是如果由于某种原因不起作用,那么这是一个django-celery(或Django本身)中的错误,应该是固定的。
您的路由器将看起来像这样:
class CeleryRouter(object):
将Celery模型分割为DB。
APPS =(
'django',#来自kombu.transport.django的模型,如果您使用Django作为消息传输
'djcelery',
)
DB_ALIAS ='芹菜'
def db_for_read(self,model,** hints):
如果model._meta.app_label在self.APPS中:
return self.DB_ALIAS
返回无
def db_for_write(self,model,** hints):
if self.APPS中的model._meta.app_label:
return self.DB_ALIAS
return无
def allow_relation(self,obj1,obj2,** hints):
if(obj1._meta.app_label in self.APPS and
obj2._meta .app_label in self.APPS):
return True
return None
def allow_syncdb(self,db,model):
如果db == self.DB_ALIAS:
#只将模型从APPS放入Celery表(南方为
#migrations)。
返回model._meta.app_label in self.APPS +('south',)
elif model._meta.app_label in self.APPS:
#不要把芹菜模型放在别的地方。
return False
return无
然后将其添加到您的设置中: p>
DATABASE_ROUTERS = ['path.to.CeleryRouter']
Is it possible to set a different database to be used with Django Celery?
I have a project with multiple databases in configuration and don't want Django Celery to use the default one.
I will be nice if I can still use django celery admin pages and read results stored in this different database :)
It should be possible to set up a separate database for the django-celery models using Django database routers:
https://docs.djangoproject.com/en/1.4/topics/db/multi-db/#automatic-database-routing
I haven't tested this specifically with django-celery, but if it doesn't work for some reason, then it's a bug in django-celery (or Django itself) that should be fixed.
Your router would look something like this:
class CeleryRouter(object):
"Route Celery models to separate DB."
APPS = (
'django', # Models from kombu.transport.django, if you're using Django as a message transport.
'djcelery',
)
DB_ALIAS = 'celery'
def db_for_read(self, model, **hints):
if model._meta.app_label in self.APPS:
return self.DB_ALIAS
return None
def db_for_write(self, model, **hints):
if model._meta.app_label in self.APPS:
return self.DB_ALIAS
return None
def allow_relation(self, obj1, obj2, **hints):
if (obj1._meta.app_label in self.APPS and
obj2._meta.app_label in self.APPS):
return True
return None
def allow_syncdb(self, db, model):
if db == self.DB_ALIAS:
# Only put models from APPS into Celery table (and south for
# migrations).
return model._meta.app_label in self.APPS + ('south',)
elif model._meta.app_label in self.APPS:
# Don't put Celery models anywhere else.
return False
return None
Then add this to your settings:
DATABASE_ROUTERS = ['path.to.CeleryRouter']
这篇关于Django Celery和多个数据库(Celery,Django和RabbitMQ)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!