简体   繁体   中英

Remove Database settings of django using bash script

I am trying to create a bash script to create a boilerplate django project that suits for my company. I need to delete the DATABASES in settings and append new one. The DATABASES is a python dictionary with structure

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

For this I tried something like this

sed -i -e "/DATABASES = {/,/}/d" settings.py

But it resulted in a trailing }.

The output is

}

I understood that the pattern it matches is for the first curly braces but not the second. What should be the approach for this.

Why not just append a new DATABASE Configuration at the end of settings? It will automatically override previous DATABASE configuration. I use like this:

    DBCONIG="DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'NAME': '${dbName}',
            'USER': '${dbUName}',
            'PASSWORD': '${dbPass}',
            'HOST': '127.0.0.1',
            'PORT': '3306',

        }
    }"
    echo $DBCONIG >> settings.py

Here I collection dbName , dbUName etc from shell input.

For cleaner implementation approach, I keep a local_settings.py which has user machine specific setups. Inside in local_settings.py , I put the user's DB configurations. and I import this file in settings.py like this:

try:
   from .local_settings import *
except:
   pass

Also, I usually put local_settings.py file in .gitignore , so that these settings are not pushed to repository.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM