lundi 2 novembre 2015

How can I make Django choose a specific default database when it starts up with command line argument

I want Django to use a specific database(sqlite) as default by an argument given when It starts up regardless of settings.py. For this purpose, I added below code in manage.py so that settings.DATABASES can be set with the argument when it starts up.

# manage.py

from myapp import settings
settings.DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(settings.DB_PATH, sys.argv[3]),
    },
}

But It doesn't work....Django still accesses origin database represented in settings.py

I think it is because Django already has done with setting default DB before I change settings.DATABASES in manage.py.

As a result, the change doesn't have any effect.

When does Django choose default database with settings? If I can change settings.DATABASES before Django choose default database, I will work i think.

Is there any way to change default database at runtime? So that even Django choose already default database I can change it.

It doesn't mean to change database dynamically using `model.objects.using('dbname').

Aucun commentaire:

Enregistrer un commentaire