'dan 1 veritabanına taşıyın Tabloları seçici olarak 1 veritabanında oluşturmaya çalışıyorum, ancak diğerini değil.Tabloları yalnızca Django
Örneğimde, yalnızca uygulamalar için tablolar oluşturmak istiyorum: tlocation
ve tcategory
transforms
veritabanında. Ancak Django, transforms
veritabanındaki tüm tabloları yaratıyor.
TRANSFORM_APPS = ('tcategory', 'tlocation')
class TransformRouter(object):
"""
A router to control all database operations on models in the
"utils_transform" application.
"""
def db_for_read(self, model, **hints):
"""
Attempts to read 'transforms' models go to 'transforms' database.
"""
if model._meta.app_label in TRANSFORM_APPS:
return 'transforms'
return None
def db_for_write(self, model, **hints):
"""
Attempts to write 'transforms' models go to 'transforms' database.
"""
if model._meta.app_label in TRANSFORM_APPS:
return 'transforms'
return None
def allow_relation(self, obj1, obj2, **hints):
"""
Allow relations if a model in the 'tlocation' app is involved.
"""
if obj1._meta.app_label in TRANSFORM_APPS or \
obj2._meta.app_label in TRANSFORM_APPS:
return True
return None
def allow_migrate(self, db, app_label, model=None, **hints):
"""
Make sure the 'tlocation' app only appears in the 'transforms'
database.
"""
if app_label in TRANSFORM_APPS:
return db == 'transforms'
return None
class DefaultRouter(object):
"""
Catch-all Router for all other DB transactions that aren't in the
``utils_transform`` app.
"""
def db_for_read(self, model, **hints):
return 'default'
def db_for_write(self, model, **hints):
return 'default'
def allow_relation(self, obj1, obj2, **hints):
if obj1._state.db == obj2._state.db:
return True
return None
def allow_migrate(self, db, app_label, model=None, **hints):
return None
Ben göçler çalıştırmak için kullanıyorum komut şöyledir::
./manage.py migrate --database=transforms
Ben sadece, aşağıda bir seferde 1 uygulama geçirirken, bu İşte
DB yönlendirici yapılandırma olduğunu Eserleri. Ancakmigrate
komutunda bir uygulama filtresi olmadan çalışamıyorum. Örnek:
./manage.py migrate tlocation --database=transforms
./manage.py migrate tcategory --database=transforms
Gerçekten tabloları oluşturduğunu onayladınız mı? Çünkü "göçme" nin çıkışı her iki durumda da aynı gözüktüğü için, "bu" False "i döndüren bir model üzerinde sessizce hiçbir işlem yapmaz. –
@KevinChristopherHenry, evet, bu tabloları oluşturuyor –
@AaronLelevier DATABASE_ROUTERS ayarında yönlendiricilerin sırası nedir? –