What I have:
我拥有的:
from django.conf import settings
def settings_to_dict(settings)
cfg = {
'BOTO3_ACCESS_KEY': settings.BOTO3_ACCESS_KEY,
'BOTO3_SECRET_KEY': settings.BOTO3_SECRET_KEY,
# repeat ad nauseum
}
return cfg
instance = SomeClassInstantiatedWithADict(**settings_to_dict(settings))
What I'd like (using Django 1.11):
我想要什么(使用Django 1.11):
from django.conf import settings
instance = SomeClassInstantiatedWithADict(**settings.to_dict())
I've tried:
from django.conf import settings
instance = SomeClassInstantiatedWithADict(**settings.__dict__)
which is close, but __dict__
only gets a small subset of the settings, which I assume are hard coded ones as opposed to added attributes. Thanks for any help!
虽然很接近,但是__dict__只获得了一小部分设置,我认为它们是硬编码的,而不是添加属性。谢谢你的帮助!
4 个解决方案
#1
3
Use the following code:
使用以下代码:
from django.conf import settings
instance = settings.__dict__['_wrapped'].__dict__
Then you will have the whole settings dict in instance
as dictionary.
然后你将整个设置dict作为字典实例。
#2
0
This turns the entire settings object into a dictionary:
这会将整个设置对象转换为字典:
In [12]: settings_dict = {}
In [13]: for st in dir(settings):
...: if st.startswith('_'):
...: continue
...: settings_dict[st] = getattr(settings, st)
...:
...:
In [14]: settings_dict
This is very exhaustive and it contains all possible attributes of settings.
这非常详尽,它包含所有可能的设置属性。
dir()
is a Python builtin. Read about it here
dir()是一个内置的Python。在这里阅读它
#3
0
Check settings.__dict__['_wrapped'].__dict__
in your shell. It seems there is everything inside. Not sure however if it's the right way to access this.
在你的shell中检查设置.__ dict __ ['_ wrapped'] .__ dict__。似乎里面有一切。但是不确定这是否是访问它的正确方法。
#4
0
>>> from django.conf import settings
>>> settings_dict = vars(settings._wrapped)
>>> print(settings_dict)
{'ABSOLUTE_URL_OVERRIDES': {}, 'ADMINS': [], 'ALLOWED_HOSTS': ['127.0.0.1', 'localhost', 'www.tetricoins.com', 'api.tetricoins.com'], 'APPEND_SLASH': True, ...
#1
3
Use the following code:
使用以下代码:
from django.conf import settings
instance = settings.__dict__['_wrapped'].__dict__
Then you will have the whole settings dict in instance
as dictionary.
然后你将整个设置dict作为字典实例。
#2
0
This turns the entire settings object into a dictionary:
这会将整个设置对象转换为字典:
In [12]: settings_dict = {}
In [13]: for st in dir(settings):
...: if st.startswith('_'):
...: continue
...: settings_dict[st] = getattr(settings, st)
...:
...:
In [14]: settings_dict
This is very exhaustive and it contains all possible attributes of settings.
这非常详尽,它包含所有可能的设置属性。
dir()
is a Python builtin. Read about it here
dir()是一个内置的Python。在这里阅读它
#3
0
Check settings.__dict__['_wrapped'].__dict__
in your shell. It seems there is everything inside. Not sure however if it's the right way to access this.
在你的shell中检查设置.__ dict __ ['_ wrapped'] .__ dict__。似乎里面有一切。但是不确定这是否是访问它的正确方法。
#4
0
>>> from django.conf import settings
>>> settings_dict = vars(settings._wrapped)
>>> print(settings_dict)
{'ABSOLUTE_URL_OVERRIDES': {}, 'ADMINS': [], 'ALLOWED_HOSTS': ['127.0.0.1', 'localhost', 'www.tetricoins.com', 'api.tetricoins.com'], 'APPEND_SLASH': True, ...