0

is there a way to create database from django manage command ? i want create a multitenant Application and need to generate dynamic database configuration for django manage command.

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    },
   **'another_generated_from_manage_command'**:{
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}

}
ehsan safir
  • 43
  • 1
  • 3
  • you want to create for each tenant its own database ? – cizario Jul 05 '20 at 11:56
  • yes I want to create a database foe each tenant dynamic – ehsan safir Jul 05 '20 at 12:35
  • 2
    i think, in your context, for a given tenant, upon his signup, the database should be then **automatically** created, via **template of database schema** not via `django` manage command ? but if you need it for some reasons have a look at this topic https://docs.djangoproject.com/en/3.0/topics/db/multi-db/#defining-your-databases – cizario Jul 05 '20 at 12:53

1 Answers1

0

I am using multi-database one for development and other for production. However, I've never seen a command that is generated and configured by a python command.

Therefore, this is the approach I have done:

if 'DATABASE_URL' in os.environ:
    #Deployed DB
    DATABASES = {
        'default': dj_database_url.parse(os.environ.get('DATABASE_URL'))
    }
else:
    #Local DB
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.sqlite3',
            'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
            }
        }
Elias Prado
  • 1,518
  • 2
  • 17
  • 32