0

From the docs, it looks like the database needs to be present in the settings.py. Is there a way to pass the database details while running python manage.py runserver given the migrations have been completed?

Edit -

Use case - The database is not known beforehand so I can't have it hardcoded in settings.py. Also, there will be one database for the entire app.

  1. Using a settings file other than settings.py in Django - This questions answers how to use a settings file different than settings.py. However, I am looking for ways in which I don't use settings file to specify the database credentials.
user2851669
  • 341
  • 1
  • 4
  • 18

1 Answers1

1

split your settings.py into two files

  • base_settings.py (add all the common settings here)
  • development.py (add your database settings here)

like :

from .base_settings import *

DATABASES = {
    'default': {
        'ENGINE': 'XX.db.backends.postgresql',
        'NAME': 'XX',
        'USER': 'postgres',
        'ATOMIC_REQUESTS':True,
        'PASSWORD': '*****',
        'HOST': '0.0.0.0',
        'PORT': '5432',
    }
}

and run your project using

python manage.py runserver 0.0.0.0:8002 --settings=django_project_name.development
Lubdhak
  • 106
  • 1
  • 8
  • If I could pass arguments to the development.py(or base_settings.py), then that would be the best solution since I will not have to modify the files at runtime. – user2851669 Aug 28 '18 at 08:42
  • try if you can set a .json file as config file and read the values of it. – Lubdhak Sep 03 '18 at 15:32