Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
317 views
in Technique[技术] by (71.8m points)

python - multiple databases and multiple models in django

I have two databases and two models:the Admin and the user.

I want to sync my models to the two databases; admin model to database A and user model to database B;

If I am setting the model path to INSTALLED_APPS and syncdb, the two models will sync to the default database.

if I set the database in the syncdb command such as sync --database="B", and the two models will sync to database B.

So my problem is, how do I sync the two models to two databases?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I fully agree with @alecxe on using the database router. I am currently using a single admin interface to manage multiple databases. Note that authentication for all databases are stored in the default database, so when you do the syncdb (with no arguments).

Generic Database Router

I found this implementation to be extremely flexible and useful.

Settings.py

# Define the database manager to setup the various projects
DATABASE_ROUTERS = ['manager.router.DatabaseAppsRouter']
DATABASE_APPS_MAPPING = {'mux_data': 't29_db', 
                         'T50_VATC':'t50_db'}

DATABASES = {
    'default': {
            'ENGINE': 'django.db.backends.postgresql_psycopg2', 
            'NAME': 'fail_over',                    
            'USER': 'SomeUser',                      
            'PASSWORD': 'SomePassword',                  
            'HOST': '127.0.0.1',                     
            'PORT': '',                      
    },

    't29_db': {
            'ENGINE': 'django.db.backends.postgresql_psycopg2', 
            'NAME': 'mux_stage',                    
            'USER': 'SomeUser',                      
            'PASSWORD': 'SomePassword',                  
            'HOST': '127.0.0.1',                      
            'PORT': '',                      
    },

    't50_db': {
            'ENGINE': 'django.db.backends.postgresql_psycopg2', 
            'NAME': 't50_vatc',                    
            'USER': 'SomeUser',                      
            'PASSWORD': 'SomePassword',                 
            'HOST': '127.0.0.1',                     
            'PORT': '',                      
    },
}

Sample Models

# Create your models here.
class Card_Test(models.Model):
    name = models.TextField(max_length=100)
    description = models.TextField(max_length=200)
    units = models.TextField(max_length=500)
    result_tags = models.TextField(max_length=500)

    class Meta:
        app_label = 'mux_data'

    def __unicode__(self):
        return self.name

class Status_Type(models.Model):
    status = models.CharField(max_length=25)

    class Meta:
        app_label = 'mux_data'

    def __unicode__(self):
        return self.status

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...