django-environ dictionary format
Question:
I’m having a difficult time trying to understand how to write data to the .env file as to get a dictionary.
The docs say this:
dict (BAR=key=val;foo=1.1;baz=True) #environ.Env(BAR=(dict(value=unicode,
cast=dict(foo=float,baz=bool)), {}))
In my .env file i have something like this
EMAIL=host=xx;port=xx;user=xx;pass=xx;tls=True
However, i really don’t know what to pass to the casting in the settings.py file
Answers:
In casting up the .env file vars in settings file one need this structure for dictionaries for this particula case:
env = environ.Env(
EMAIL=({
'cast':{
'host':str,
'port':int,
'user':str,
'pass':str,
'tls':bool,
}
}, {})
)
If an empty dictionary is supplied, then all the values will be cast as strings.
Two ways:
.env
DATA={"hello":"world"}
main.py
env = environ.Env()
environ.Env.read_env()
# Get the data
data = env.json("DATA")
type(data) # dict
.env
DATA=hello=world,hello2=world2
main.py
env = environ.Env()
environ.Env.read_env()
# Get the data
data = env.dict("DATA")
type(data) # dict
This works for me:
ENV:
# The value of the mockup user
AAA_MOCKUP_USER_OBJ={'is_authenticated'=True,'id'=888,'username'="sasha_test",'firstName'="Sasha",'lastName'="Rudan"}
common.py
:
print(f"[common] AAA_MOCKUP_USER_OBJ={AAA_MOCKUP_USER_OBJ}")
printes well:
[common] AAA_MOCKUP_USER_OBJ={"{'is_authenticated'": 'True', "'id'": '888', "'username'": '"sasha_test"', "'firstName'": '"Sasha"', "'lastName'": '"Rudan"}'}
I’m having a difficult time trying to understand how to write data to the .env file as to get a dictionary.
The docs say this:
dict (BAR=key=val;foo=1.1;baz=True) #environ.Env(BAR=(dict(value=unicode,
cast=dict(foo=float,baz=bool)), {}))
In my .env file i have something like this
EMAIL=host=xx;port=xx;user=xx;pass=xx;tls=True
However, i really don’t know what to pass to the casting in the settings.py file
In casting up the .env file vars in settings file one need this structure for dictionaries for this particula case:
env = environ.Env(
EMAIL=({
'cast':{
'host':str,
'port':int,
'user':str,
'pass':str,
'tls':bool,
}
}, {})
)
If an empty dictionary is supplied, then all the values will be cast as strings.
Two ways:
.env
DATA={"hello":"world"}
main.py
env = environ.Env()
environ.Env.read_env()
# Get the data
data = env.json("DATA")
type(data) # dict
.env
DATA=hello=world,hello2=world2
main.py
env = environ.Env()
environ.Env.read_env()
# Get the data
data = env.dict("DATA")
type(data) # dict
This works for me:
ENV:
# The value of the mockup user
AAA_MOCKUP_USER_OBJ={'is_authenticated'=True,'id'=888,'username'="sasha_test",'firstName'="Sasha",'lastName'="Rudan"}
common.py
:
print(f"[common] AAA_MOCKUP_USER_OBJ={AAA_MOCKUP_USER_OBJ}")
printes well:
[common] AAA_MOCKUP_USER_OBJ={"{'is_authenticated'": 'True', "'id'": '888', "'username'": '"sasha_test"', "'firstName'": '"Sasha"', "'lastName'": '"Rudan"}'}