1👍
Create one new table called ‘property_variables’ (any name).
fields
- property_name
- property_value
Instead of specifying configurations in settings.py file, save these details in ‘property_variables’ table.
Example:
property_name – EMAIL_BACKEND
property_value – ‘django.core.mail.backends.smtp.EmailBackend’
For avoiding multiple DB hit, you can cache these details in redis or somewhere else.
When you want to sent email, instead of reading config details from settings.py file, read it from cache.
If you want to change the configuration, open admin panel and change values. You can invalidate the cache after the DB update.
__init__.py
from myapp.models import MyModel
CONFIGS = dict()
def read_config_variables():
configs = MyModel.objects.all()
for config in configs:
CONFIGS[config.propertyName] = config.propertyValue
read_config_variables()
settings.py
import CONFIGS
EMAIL_BACKEND = CONFIGS.get('EMAIL_BACKEND', 'django.core.mail.backends.smtp.EmailBackend')
EMAIL_HOST = CONFIGS.get('EMAIL_HOST', 'smtp.gmail.com')
EMAIL_PORT = CONFIGS.get('EMAIL_PORT', 587)
EMAIL_HOST_USER = CONFIGS.get('EMAIL_HOST_USER', '*****@gmail.com')
EMAIL_HOST_PASSWORD = CONFIGS.get('EMAIL_HOST_PASSWORD', '*******')
EMAIL_USE_TLS = CONFIGS.get('EMAIL_USE_TLS', True)
Source:stackexchange.com