How do I sync the two models to two databases

0 votes
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?
Jun 29, 2020 in Python by kartik
• 37,520 points
1,787 views

1 answer to this question.

0 votes

Hello @kartik,

To sync the two models to two databases follow the below code:

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
answered Jun 29, 2020 by Niroj
• 82,840 points

Related Questions In Python

0 votes
1 answer

how do i print only the last two letters in a string using regular expression in python?

$ to match the end of the ...READ MORE

answered Mar 15, 2019 in Python by Waseem
• 4,540 points
932 views
0 votes
1 answer
0 votes
1 answer
–1 vote
2 answers
0 votes
1 answer

How to temporarily disable a foreign key constraint in MySQL?

Hello @kartik, To turn off foreign key constraint ...READ MORE

answered Jun 23, 2020 in Python by Niroj
• 82,840 points
2,451 views
0 votes
1 answer

How do I use Django templates without the rest of Django?

Hello @kartik, Let's say you have this important ...READ MORE

answered Jun 23, 2020 in Python by Niroj
• 82,840 points
1,565 views
0 votes
1 answer

How do I add a link from the Django admin page of one object to the admin page of a related object?

Hello @kartik, Set show_change_link to True (False by default) in your inline ...READ MORE

answered Jun 12, 2020 in Python by Niroj
• 82,840 points
10,605 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP