Search code examples
pythondjangounit-testingtestingdjango-settings

Django @override_settings does not allow dictionary?


I am new to Python decorators so perhaps I am missing something simple, here is my situation:

This works for me:

def test_something(self):
    settings.SETTING_DICT['key'] = True #no error
    ...

But this throws a "SyntaxError: keyword can't be an expression":

@override_settings(SETTING_DICT['key'] = True) #error
def test_something(self):
   ...

Just to be clear, normal use of override settings works:

@override_settings(SETTING_VAR = True) #no error
def test_something(self):
   ...

Is there a way to use the decorator with a settings dictionary, or am I doing something wrong?

Thanks in advance!


Solution

  • 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):
             ...