Multiple Database django

0

Hi, I'm new to django, I'm working on a project where I use sql server 2008 myself, but I have a problem where I'm going to put the application will not always be connected to the same database server or to the same database data because they have the same structure but not the same name, my question is how can I get by django the name of the database being on the localhost or giving an ip, greetings and I hope someone can help me

    
asked by Antonio 19.06.2018 в 21:15
source

1 answer

0

It is known that when you are doing a development where there is a database involved it is necessary to keep a file where the credentials of the database are stored, that later in production this data changes, that is why you could have within your project a file called database.py with the following structure

DATABASES = {
    'default': {
        'ENGINE': 'sqlserver_ado',
        'NAME': 'my_database',
        'USER': 'my_db_user',
        'PASSWORD': 'my_db_password',
        'HOST': 'dbserver\ss2008',
    }
}

Already in your code you import this file and call these constants to make the connection

import database

dbHost     = database.DATABASES['default']['HOST']
dbUsername = database.DATABASES['default']['USER']
dbPassword = database.DATABASES['default']['PASSWORD']
dbName     = database.DATABASES['default']['NAME']

If you are using a version control manager such as git, you can call this file database.sample.py to have it available, make a copy of your project by removing the sample from the file name and tell the repository that will ignore in the push, this so that the local configuration does not overwrite the configuration file in production

    
answered by 19.06.2018 / 23:24
source