Django @override_settings does not allow dictionary?

让人想犯罪 __ 提交于 2019-12-01 16:39:28

You should override the whole dict:

@override_settings(SETTING_DICT={'key': True})
def test_something(self):
   ...

Or, you can use override_settings as a context manager:

def test_something(self):
     value = settings.SETTING_DICT
     value['key'] = True
     with override_settings(SETTING_DICT=value):
         ...

I didn't want to override the whole dict either, so I copied the dictionary in question from the settings object and just modified the attribute I was interested in:

import copy
from django.conf import settings

settings_dict = deepcopy(settings.SETTINGS_DICT)
settings_dict['key1']['key2'] = 'new value'

@override_settings(SETTINGS_DICT=settings_dict)
def test_something(self):
    pass

It suits my purposes, but if you wanted to make this more widely usable, you could write a short function with a few arguments which could do something like this dynamically.

Note: I tried using settings.SETTINGS_DICT.copy() instead of deepcopy(settings.SETTINGS_DICT) but that seemed to globally override the setting for all tests...

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!