HBNetwork / python-decouple

Strict separation of config from code.
MIT License
2.79k stars 192 forks source link

AttributeError: 'tuple' object has no attribute 'read' #77

Closed SHxKM closed 4 years ago

SHxKM commented 5 years ago

REDIS_HOSTS in Django expects a list of tuples.

When I try to do this:

config('REDIS_URL', default=('127.0.0.1', 6379), cast=Csv(post_process=tuple))

I can't start my server, and this error is shown:

AttributeError: 'tuple' object has no attribute 'read'

henriquebastos commented 4 years ago

Try config('REDIS_URL', default='127.0.0.1, 6379', cast=Csv(post_process=tuple))

The result should be ('127.0.0.1', '6379').

The default parameter should be a string to be casted.

If you need the port number to be an integer, I would recommend something like:

# casts.py
def redis_url(s):
    parts = s.split(':')
    return tuple(parts[0], int(parts[1])

# settings.py
from casts import redis_url
REDIS _URL= config('REDIS_URL', default='127.0.0.1:6379', cast=redis_url)