django-environ
django-environ copied to clipboard
Casting Defaults; Question regarding ideal practice?
I was a little curious - in all the documentation it says to do something like this:
env = environ.Env(
# set casting, default
DEBUG=(bool, False),
)
DEBUG=env("DEBUG")
# I am adding these two for example purposes similar to documentation style
ALLOWED_HOSTS = env("ALLOWED_HOSTS")
SECRET_KEY = env("DJANGO_SECRET_KEY")
Is there any benefit to casing and/or setting defaults to other things in the environ.Env()
call? For example:
env = environ.Env(
# set casting, default
DEBUG=(bool, False),
ALLOWED_HOSTS=(list, []),
SECRET_KEY=(str, ""),
)
DEBUG=env("DEBUG")
ALLOWED_HOSTS = env("ALLOWED_HOSTS")
SECRET_KEY = env("DJANGO_SECRET_KEY")
Is there any benefit to one way over the other? Is one better than the other or are they identical?
Or would something like this be better?
# default method, but casting the variables when setting them
env = environ.Env()
DEBUG=env.bool("DEBUG", default=False)
ALLOWED_HOSTS = env.list("ALLOWED_HOSTS", default=[])
SECRET_KEY = env.str("DJANGO_SECRET_KEY", default="")
Just looking for thoughts on which is the 'ideal' way - any advice appreciated!
Also interested. If there is any significant difference, it would be great to know in order to improve docs.
FYI this is what I do: https://blog.enriquesoria.com/mantainable-env-sample/