Skip to content

Instantly share code, notes, and snippets.

@Biazus
Last active February 14, 2023 17:18
Show Gist options
  • Save Biazus/e8cb186b67d711a50313102115534081 to your computer and use it in GitHub Desktop.
Save Biazus/e8cb186b67d711a50313102115534081 to your computer and use it in GitHub Desktop.
Context Manager to override django settings
from django.conf import settings
class OverrideSetting(object):
"""
Enable the temporary change of a setting. Useful for disabling settings when testing or creating fixtures on dev
environment.
CAUTION: Do not use it to change production settings.
"""
def __init__(self, key, value):
self.key = key
self.old_setting_value = getattr(settings, self.key)
setattr(settings, self.key, value)
def __enter__(self):
return settings
def __exit__(self, type, value, traceback):
setattr(settings, self.key, self.old_setting_value)
return True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment