I've created a django project and then two apps, app1 and app2. I want that both apps share a mysql database('nameofDB' to mysql, 'mydb' to django). I added the database to DATABASES in settings.py and for each app I created a dbrouter file, and added each router to DATABASE_ROUTERS. Also in settings.py added each app to INSTALLED_APPS.
My problem is when I try to make
python manage.py syncdb --database=mydb
Because it doesn't sync both apps(only app1). It says:
Creating tables ...
Creating table app1_model1
Installing custom SQL ...
Installing indexes ...
Installed 0 object(s) from 0 fixture(s)
Here my settings.py:
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app1',
'app2',
)
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
},
'mydb':{
'ENGINE': 'django.db.backends.mysql',
'NAME': 'nameofDB',
'USER':'username',
'PASSWORD':'password',
}
}
DATABASE_ROUTERS = ['app1.dbRouter.App1DBRouter', 'app2.dbRouter.App2DBRouter']
Here my models:
app1/models.py:
class Model1(models.Model):
name = models.CharField(max_length=100)
app2/models.py:
class Model2(models.Model):
name = models.CharField(max_length=100)
Here are my dbrouters
app1/dbRouter.py
class App1DBRouter(object):
def db_for_read(self,model, **hints):
if model._meta.app_label == 'app1':
return 'mydb'
return None
def db_for_write(self,model, **hints):
if model._meta.app_label == 'app1':
return 'mydb'
return None
def allow_relation(self,obj1, obj2, **hints):
if obj1._meta.app_label == 'app1' and \
obj2._meta.app_label == 'app1':
return True
return None
def allow_syncdb(self,db, model):
if db == 'mydb':
return model._meta.app_label == 'app1'
elif model._meta.app_label == 'app1':
return False
return None
app2/dbRouter.py
class App2DBRouter(object):
def db_for_read(self,model, **hints):
if model._meta.app_label == 'app2':
return 'mydb'
return None
def db_for_write(self,model, **hints):
if model._meta.app_label == 'app2':
return 'mydb'
return None
def allow_relation(self,obj1, obj2, **hints):
if obj1._meta.app_label == 'app2' and \
obj2._meta.app_label == 'app2':
return True
return None
def allow_syncdb(self,db, model):
if db == 'mydb':
return model._meta.app_label == 'app2'
elif model._meta.app_label == 'app2':
return False
return None
What's wrong with it? What should I do? Thanks in advance! :)