django-db-settings is a Django app to save your configuration in cacheable DB objects that are easily defined like classes.
- Install
django-db-settingsusingpip:
$ pip install django-db-settings- Add "settings" to your INSTALLED_APPS setting like this::
INSTALLED_APPS = [
...
'settings',
]- Include the settings URLconf in your project urls.py like this::
path('settings/', include('settings.urls')),-
Run
python manage.py migrateto create the settings models. -
Start the development server and visit http://127.0.0.1:8000/admin/ to setup your app settings (you'll need the Admin app enabled).
-
Visit http://127.0.0.1:8000/settings/?setting=YOUR_SETTING to get the objects related to that specific setting (JSON).
-
Find the REFRESH SETTINGS button with in Value model change list page. This project uses TTL based cache, which can be configured by adding the following setting:
- SETTINGS_CACHE_MAXSIZE: To set the maximum size of total items in the cache. By default set to 100.
- SETTINGS_CACHE_TTL: To set the Time To Live of the cache items. By default set to 3600 seconds (1 hour).
django-db-settings saves your settings in a flexible model. To create a setting follow these steps:
-
Create a setting in model
Setting, it will act like the class that will group a set of settings. e.g.,product type -
Define the attributes of this
Settingby adding them in modelField. You can set each as public or private. e.g.,title,description,code. -
Add
Settinginstances in modelInstance. They will act as the objects of the setting class. e.g.,saving,credit card,loan. -
Finally, add values in model
Valuefor every instance guided by the fields defined forSetting. e.g., forsavinginstance, values aretitle: Saving account,description: Saves you money,code: S001.
After adding all values, you will be able to retrieve all those which fields are public by going to http://127.0.0.1:8000/settings/?setting=product%20type. It returns a JSON object:
{"saving": {"title": "Saving account", "description": "Save your money with us!"}, "credit card": {"title": "Credit Card", "description": "Get the best from our Credit Card"}, "loan": {"title": "Loan", "description": "We loan you the money you need"}}TIP: You can use the same settings internally by calling method get_setting in module settings.business:
>>> from settings.business import get_setting
>>> get_setting('product type')
{'saving': {'title': 'Saving account', 'description': 'Save your money with us!'}, 'credit card': {'title': 'Credit Card', 'description': 'Get the best from our Credit Card'}, 'loan': {'title': 'Loan', 'description': 'We loan you the money you need'}}Internally, you can set parameter include_non_public=True to retrieve private fields also:
>>> get_setting('product type', include_non_public=True)
{'saving': {'title': 'Saving account', 'description': 'Save your money with us!', 'code': 'S-001'}, 'credit card': {'title': 'Credit Card', 'code': 'C-001', 'description': 'Get the best from our Credit Card'}, 'loan': {'title': 'Loan', 'description': 'We loan you the money you need', 'code': 'L-001'}}Call method clear_settings_cache from the same package to clear the cache and refresh the global settings.
>>> from settings.business import clear_settings_cache
>>> clear_settings_cache()
True