28đź‘Ť
AFAIK, the Django settings are supposed to be immutable. There are multiple reasons for this, the most obvious being that Django is not aware of the server’s execution model (prefork / multi-threaded).
Also, you can’t load the settings themselves from a Django model because the settings need to be loaded before you can use anything in the ORM.
So, basically, you have two solutions:
- you can bootstrap the settings from the database by using any lower-level database access mechanism to load them; or
- you can just define your settings in some other model and fetch them directly when you need them.
The first is an incredible hack and I don’t suggest it. The second is much more direct and cleaner, but requires you to change your usual habits (from django.conf import settings
).
The second approach is probably what’s implemented by the 3rd-party apps you linked to.
17đź‘Ť
From Django 1.8 docs:
You shouldn’t alter settings in your applications at runtime.
- [Django]-How to convert JSON data into a Python object?
- [Django]-Unsupported operand type(s) for *: 'float' and 'Decimal'
- [Django]-Django – Model graphic representation (ERD)
11đź‘Ť
DATABASES
is a dict. So you can manipulate how a dictionary:
import django.conf as conf
conf.settings.DATABASES['default']['NAME'] = 'novo_banco'
- [Django]-How do I get user IP address in Django?
- [Django]-What should I use instead of syncdb in Django 1.9?
- [Django]-How to convert JSON data into a Python object?
2đź‘Ť
Take a look: https://bitbucket.org/bkroeze/django-livesettings
*Django-Livesettings is a project split from the Satchmo Project
_. It provides the ability to configure settings via an admin interface, rather than by editing “settings.py”.*
Maybe it can be helpful for you.
- [Django]-Combining Django F, Value and a dict to annotate a queryset
- [Django]-Django – makemigrations – No changes detected
- [Django]-How can I enable CORS on Django REST Framework
2đź‘Ť
Honestly I get more Django when I analyze his code. In version 1.4.5 did it (following the module below):
-
myproject\manage.py
-
django\core\management__init__.py ## method – execute_manager
-
django\conf__init__.py ## class – LazySettings; attr – _wrapped
-
django\utils\functional.py ## class LazyObject; important method –
new_method_proxy
Functional option, but it has its risks. In the python “_” considers the attribute as protected.
from django.conf import settings
settings._wrapped.INSTALLED_APPS = () ## *really work*
In the following project: https://github.com/alexsilva/DJPlugins
you can see this variable being modified at runtime. the idea of the project is already working.
- [Django]-How to set environment variables in PyCharm?
- [Django]-What is the benefit of using a HyperlinkedModelSerializer in DRF?
- [Django]-How can I access environment variables directly in a Django template?
0đź‘Ť
You cannot directly modify the settings.py file
For example:
If u want change the database at runtime, you should Separate the configuration of the database
# Projecr_name/user_database.py
user_database = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'name',
'USER': 'admin',
'PASSWORD': '111111',
'HOST': '127.0.0.1',
'PORT': '3306'
},
'user_db': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'user1',
'USER': 'admin',
'PASSWORD': '22222',
'HOST': '127.0.0.1',
'PORT': '3306'
}
}
# Projecr_name/settings.py
from .user_database import user_database
...
DATABASES = user_database
...
Call in your logical view
# view.py
from ../Projecr_name/user_database import user_database
class Some(...):
def Some(request):
user_database['user_db']['NAME']='user2'
then you can change any setting at runtime use this way
- [Django]-Django: using more than one database with inspectdb?
- [Django]-Django or Django Rest Framework
- [Django]-Custom QuerySet and Manager without breaking DRY?
0đź‘Ť
If you have a variable in the settings.py and you want it to change at any time there is 2 ways the
First one is to make a table in database then make for it a serializer then make a view set and any time you want to get it you could send a http request to this viewset then retrieve the data
Second one is to use caching it is so fast and it is familiar to localStorage in angular
- [Django]-How to get getting base_url in django template
- [Django]-Why does my Django admin site not have styles / CSS loading?
- [Django]-Django edit user profile
-1đź‘Ť
You can use recomended .configure()
method of settings
module:
from django.conf import settings
settings.configure(DEBUG=True)
settings
module has additional handy features. Check docs.
- [Django]-How do I create multiple model instances with Django Rest Framework?
- [Django]-How to call function that takes an argument in a Django template?
- [Django]-Django gives Bad Request (400) when DEBUG = False