I am a new in django. I storage global application variables inside settings environment folder. But now i need store variables inside Database because i want changes them from django admin. My way to do this: Create model class in django core, where define two variable app_name - list of application and data-JSON. But i think this is not best praxis. Since it may be difficult to use the GIT
Decided to use a ready-made solution: django-extra-setting.
In apps setting define variables that will be storage in DB:
DB_VARIABLES = {'variable_name': ['text', 'default_value']}
Reads a variable from files after django project has been loaded
from settings import PROJECT_APPS
from extra_settings.models import Setting
from importlib import import_module
class CoreConfig(AppConfig):
def ready(self):
if (is_manage_py and is_runserver) or (not is_manage_py):
local_settings = import_module(f'path/{app_name}')
for app_name in PROJECT_APPS:
local_constants = local_settings.DB_VARIABLES if hasattr(local_settings, 'DB_VARIABLES') else False
if local_constants:
variables = {**variables, **{key: local_constants[key] for key in local_constants}}
for constant_name in local_constants:
if Setting.objects.filter(name=constant_name).count() == 0:
constant_values = local_constants[constant_name]
Setting(name=constant_name, value_type=constant_values[0], value=constant_values[1]).save()