Package: django-json-settings2
django-json-settings2Info:Simple application to store Django settings in a json file.Version:0.1.0Author:Team Netlandish (https://www.netlandish.com)Python / Django SupportPython 3.6+ for Django versions 1.11+Truthfully, this app is so simple it will probably work with previous
version of Python and Django but we can’t promise that.Why?I know, right? Who needs another way to store settings outside of the
standard Djangosettings.pysetup.None of the existing ways actually fit our typical Django deployment
setup in a way that was satisfactory. This method allows us to store
settings externally and in a way that fits our needs. Maybe it’ll
fit yours too.Also, there is already adjango-json-settingsapp and while that app
very well may be perfect for your project, it isn’t a good fit for ours.We created this app years ago and simply tuned it slowly as needed. It’s
very simple yet flexible enough to work within virtually any workflow.This app is really nothing more than helper functins wrapped on top
of standardjsonmodule ops.OthersThere are several other options to store settings outside of the typical
Djangosettings.pyfile. Here are a few:https://github.com/isotoma/django-json-settingshttps://github.com/theskumar/python-dotenvThere’s dozens of others. Pick the one that best suits your needs.UsageSaving SettingsYou’re going to need to save your desired settings to a json file
first. There’s a simple helper function, and management command,
included to help.For instance, say you want to create a simple setting for yourSECRET_KEYvariable:$ python
>>> settings_to_save = ['SECRET_KEY']
>>> from json_settings2 import write_settings_from_django
>>> write_settings_from_django(*settings_to_save)
>>> exit()
$ cat .settings.json
{
    "SECRET_KEY": "SUPER SECRET KEY IS HERE! COOOOOOOLLLLL!"
}
$Thewrite_settings_from_djangofunction takes a few optional variables:settings_vars = Positional args giving every Django setting to savefilename = Filename of the json settings file. Defaults to.settings.jsondirectory = Directory in which to savefilename. Defaults to..indent = Indentation level for the json output. Set toNonefor the mostcompact file. Defaults to4.force = Ifdirectory/filenameexists, overwrite it.Defaults toFalseYou can also just use the management command. This requires that you placejson_settings2in yourINSTALLED_APPSsetting:$ python manage.py write_json_settings SECRET_KEYYou can add as many settings as you’d like too:$ python manage.py write_json_settings SECRET_KEY DATABASES STATIC_URLTo see the options, simply:$ python manage.py help write_json_settingsLoading SettingsThe easiest way is to store all your default and local settings insettings.pyand load the json settings at the end. It’s pretty
straight forward. Let’s see an example:$ cat settings.py
import os
from json_settings2 import load_settings

DEBUG = True
STATIC_URL = '/static/'
... LOTS OF OTHERS SETTINGS HERE ...

SETTINGS_DIR = os.path.dirname(os.path.abspath(__file__))
load_settings(globals(), directory=SETTINGS_DIR, depth=3)Essentially this will tell the function to look for the settings file
starting in the same directory assettings.pyand if not found,
look up to 3 levels higher in the directory tree. So let’s say the
path if yoursettings.pyfile is/home/user/app/current/code/settings.pyTheload_settingsfunction will search the following paths for.settings.json:/home/user/app/current/code//home/user/app/current//home/user/app//home/user/Useful if you want to store your settings outside of the code deployment
directories, which is often the case.Theload_settingsfunction takes the following variables:current_settings - Dictionary that will be updated with found settings.Generally you’d pass inglobals().filename - Name of json file with settings. Defaults to.settings.jsondirectory - Path of the directory wherefilenamelives. Defaults to..depth - Number of parent directories to scan forfilename. Defaults to0.store = Store settings into thecurrent_settingsdict. Defaults toTrue.Ifstoreis set toFalsethen thecurrent_settingsdict will not
be altered.The function will always return the pythonic representation of what was found
in the json settings file.Note on directory- By default, thedirectoryvariable above is
set to.- meaning current directory. This usually means the directory
where you started the Python interpreter or are runningmanage.pyfrom.
This is usually NOT what you want. It’s best practice to always set the
expected directory to avoid troubleshooting headaches.What Is Used As a Setting?When callingload_settingsyou can include extra data in your json
settings file that is useful for other puposes in your code but is not
something you want cluttering yourdjango.conf.settingsobject.Only keys that are stored in all capital letters will be stored
to thecurrent_settingsdict. So if your json settings has options
that are not all caps, they will only be returned as part of the loaded
json data.In other words, say youload_settingson the following data:{
    "SeCreT_Key": "This will not be saved in Django settings.",
    "SECRET_KEY": "This WILL be saved in Django settings.",
    "secret_key": :This will not be saved in Django settings."
}YourSECRET_KEYsetting will be set toThis WILL be saved in Django settings.Where To Load Settings?Normally you can place it at the bottom of thesettings.pyfile.
However, there are often times that you need those settings to guide
the values of other settings.There is nothing stopping you from loading your json settings from
anywhere in the process. It’s up to you. Just remember that if you
load your settings and then set a duplicate variable AFTER loading
the json settings, the duplicate variable will have the final say.For example:$ cat .setting.json
{
    "STATIC_URL": "/my/cool/static/url/"
}
$ cat settings.py
import os
from json_settings2 import load_settings

SETTINGS_DIR = os.path.dirname(os.path.abspath(__file__))
load_settings(globals(), directory=SETTINGS_DIR, depth=3)

DEBUG = True
STATIC_URL = '/static/'
... LOTS OF OTHERS SETTINGS HERE ...The value of yourSTATIC_URLsetting will be set to/static/when
you might be expecting it to be/my/cool/static/url/. Just a heads up.Copyright & WarrantyAll documentation, libraries, and sample code are
Copyright 2019 Netlandish Inc. <hello@netlandish.com>. The library and
sample code are made available to you under the terms of the BSD license
which is contained in the included file, BSD-LICENSE.Commercial SupportThis software, and lots of other software like it, has been built in support of many of
Netlandish’s own projects, and the projects of our clients. We would love to help you
on your next project so get in touch by dropping us a note athello@netlandish.com.