django-environ icon indicating copy to clipboard operation
django-environ copied to clipboard

Casting Defaults; Question regarding ideal practice?

Open hannylicious opened this issue 2 years ago • 2 comments

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!

hannylicious avatar Feb 08 '23 15:02 hannylicious

Also interested. If there is any significant difference, it would be great to know in order to improve docs.

pataquets avatar Mar 13 '24 23:03 pataquets

FYI this is what I do: https://blog.enriquesoria.com/mantainable-env-sample/

EnriqueSoria avatar Jun 19 '24 09:06 EnriqueSoria