You want getattr
, not setattr
.
db = getattr(connection,dbname)
where setattr
is a way to set an attribute if you know it's name, getattr
is a way to get an attribute if you know it's name.
In other words, the following are 100% equivalent:
db = connection.DBNAME
db = getattr(connection,'DBNAME')