I am trying to setup celery in one of my django projects. I want celery to use a separate database. Currently, as the project is in development phase we are using sqlite3. In order to setup multiple databases i did the following.
Defined databases in the settings.py file.
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':'',
},
}
Created a Router Object in db_routers.py file
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
Updated the DATABASE_ROUTER variable in settings.py file
DATABASE_ROUTERS = [
'appname.db_routers.CeleryRouter',
]
Now, when i do python manage.py syncdb i see that the tables are created for celery but there is only one database created i.e. devel. Why are the tables being created in the devel database and not in celery database ?