Settings not loading if you import from an app in settings.py #8854
-
So I'm having this weird behavior where IF I import something from an app in settings.py, DRF fails to load the settings. Here's a quick and dirty example taken from my case specifically: # 'core' app / utilities.py ------------------------------------------------
import os
ENV = os.environ
def get_env(var: str, default: Optional[T]) -> str | T:
try:
return ENV[var]
except KeyError:
if default is None:
raise
return default
# settings.py -------------------------------------------------------------
from core.utilities import get_env
SECRET_KEY = get_env("SECRET_KEY")
# ... other settings ...
REST_FRAMEWORK = {
"EXCEPTION_HANDLER": "drf_standardized_errors.handler.exception_handler",
"DEFAULT_AUTHENTICATION_CLASSES": ("rest_framework_simplejwt.authentication.JWTAuthentication",),
}
# ... other settings ... With this setup, DRF settings will not load (checking the app_config.user_settings returns an empty dictionary). Simply moving the function I will note that this appears to only happen with DRF settings and not ALL Django settings. Checking Django's settings object, the REST_FRAMEWORK property is there and correct. This appears to be something very similar to what has been reported here: #5786 Unfortunately I could not find any other references to this issue, or solutions for that matter. I'm hoping that maybe this is just some dumb thing that I don't know how to google for. Tomorrow I can try and post a minimal reproducible example - but wanted to see if someone else had ran into a similar issue before. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
After digging deep, I found the issue. Turns out it was some sort of "circular import", as in that loading the imported function loaded some DRF stuff "hidden" away in an To clarify: this was an issue in my end where I was importing things from the DRF module BEFORE the REST_FRAMEWORK setting being declared/exectuted. |
Beta Was this translation helpful? Give feedback.
After digging deep, I found the issue. Turns out it was some sort of "circular import", as in that loading the imported function loaded some DRF stuff "hidden" away in an
__init__.py
file without reaching DRF settings yet.To clarify: this was an issue in my end where I was importing things from the DRF module BEFORE the REST_FRAMEWORK setting being declared/exectuted.