python - dynamically specifying variable name in a function -


i making custom django template tag.

the rudimentary version:

import models  @register.filter def intensity1(value):     return models.choices_foo[value] 

it returns value'th element of model.foo list

but have few different lists, models.choices_bar, models.choices_baz

so thought make more generalized function, pass value , arg specifies variable list refer to.

@register.filter def intensity2(value, arg):     return models.arg[value]   arg = "intensity_choices" print (intensity2(value, arg)) 

but of course doesn't work. failing module 'models' has no attribute 'arg'

is there way state that? or better off keeping these choices in dict , retrieve them referring key?

you use getattr property name:

return getattr(models, arg)[value] 

Comments

Popular posts from this blog

neo4j - finding mutual friends in a cypher statement starting with three or more persons -

php - How to remove letter in front of the word laravel -

minify - Minimizing css files -