Buitin Casts

  1. config.boolean - converts values like On|Off, 1|0, yes|no, true|false, t|f into booleans.
  2. config.eval - safely evaluate strings with Python literals to Python objects (alias to Python’s ast.literal_eval).
  3. config.list - converts comma separated strings into lists.
  4. config.tuple - converts comma separated strings into tuples.
  5. config.json - unserialize a string with JSON object into Python.
  6. config.option - get a return value based on specific options:
environments = {
    "production": ("spam", "eggs"),
    "local": ("spam", "eggs", "test"),

# Will return a tuple with ("spam", "eggs") when
# ENVIRONMENT is undefined or defined with `production`
# and a tuple with ("spam", "eggs", "test") when
# ENVIRONMENT is set with `local`.

Custom casts

You can implement your own custom casting function:

def number_list(value):
    return [int(v) for v in value.split(";")]

NUMBERS = config("NUMBERS", default="1;2;3", cast=number_list)

Useful third-parties casts

Django is a popular python web framework that imposes some structure on the way its settings are configured. Here are a few 3rd party casts that help you adapt strings into that inner structures:

  • dj-database-url - Parses URLs like mysql://user:pass@server/db into Django DATABASES configuration format.
  • django-cache-url - Parses URLs like memcached://server:port/prefix into Django CACHES configuration format.
  • dj-email-url - Parses URLs like smtp:// with parameters used in Django EMAIL_* configurations.
  • dj-admins-setting - Parses emails lists for the ADMINS configuration.