Skip to content

Commit 3779fc1

Browse files
committed
Make django_settings_is_configured return the first value always
I have been seeing an issue, where it would return False initially, but later True; causing an `AttributeError` with the `mailoutbox` fixture, because `mail.outbox` was not set on the module. This can happen with imported code that changes the environment, e.g. via "wsgi/asgi.py": > os.environ.setdefault("DJANGO_SETTINGS_MODULE", "foo.settings")
1 parent 743ea26 commit 3779fc1

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

pytest_django/lazy_django.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
import pytest
99

1010

11+
_cache = {}
12+
13+
1114
def skip_if_no_django():
1215
"""Raises a skip exception when no Django settings are available"""
1316
if not django_settings_is_configured():
@@ -21,10 +24,15 @@ def django_settings_is_configured():
2124
configured flag in the Django settings object if django.conf has already
2225
been imported.
2326
"""
24-
ret = bool(os.environ.get("DJANGO_SETTINGS_MODULE"))
27+
try:
28+
return _cache["django_is_configured"]
29+
except KeyError:
30+
ret = bool(os.environ.get("DJANGO_SETTINGS_MODULE"))
31+
32+
if not ret and "django.conf" in sys.modules:
33+
ret = sys.modules["django.conf"].settings.configured
2534

26-
if not ret and "django.conf" in sys.modules:
27-
return sys.modules["django.conf"].settings.configured
35+
_cache["django_is_configured"] = ret
2836

2937
return ret
3038

0 commit comments

Comments
 (0)