Django Enum not reflecting @override_settings()

from django.conf import settings
from django.test import TestCase

class Enum1(Enum):
    X = f'{settings.X}'

class Test1(TestCase):
    def test1(self):
        self.assertEqual(settings.X, Enum1.X.value) # Doesnt work
  • I have variable X in
  • I modify it in this test case to X='abc'
  • But the Enum is still using its old value.

Is there a way to make Enum use new value ? One workaround is if I create a func in Enum1 then it works. eg:

class Enum1(Enum):
    def get_x(cls):
        return f'{settings.X}'

The overall takeaway is that any class variable doesnt reflect override_settings() changes. Only a function which is not instantiated initially when py file gets imported, reflects those changes.

Back to Top