[Django]-Django-environ dictionary format

0👍

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.

7👍

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

0👍

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"}'}
👤mPrinC

Leave a comment