multiple databases and multiple models in django

后端 未结 2 519
终归单人心
终归单人心 2020-11-27 13:57

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;

相关标签:
2条回答
  • 2020-11-27 14:46

    In order to define specific databases used for specific models, you need to define a database router:

    The easiest way to use multiple databases is to set up a database routing scheme. The default routing scheme ensures that objects remain ‘sticky’ to their original database (i.e., an object retrieved from the foo database will be saved on the same database). The default routing scheme ensures that if a database isn’t specified, all queries fall back to the default database.

    See this snippet as an example: http://djangosnippets.org/snippets/2687/

    Also see:

    • Django multi-database routing
    • Multiple Databases in Django
    • Tutorial: Using Django's Multiple Database Support
    • Database Routers in Django
    0 讨论(0)
  • 2020-11-27 14:49

    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
    
    0 讨论(0)
提交回复
热议问题