Reputation: 30158
I currently have a bunch of language checks in place in a python (django) function:
def __get__(self, instance, owner):
if translation.get_language() == 'fr':
trans_field = getattr(instance, self.fr_field)
else:
return getattr(instance, self.en_field)
What I want to do is run this in a loop:
for language in languages:
if translation.get_language() == language:
return getattr(instance, self.[language]_field)
else:
return getattr(instance, self.en_field)
How do I do this? Obviously the self.[language]_field
is pseudocode
Upvotes: 0
Views: 90
Reputation: 78556
Use getattr
a second time:
return getattr(instance, gettattr(self, '{}_field'.format(language))
Upvotes: 2
Reputation: 5185
You already have the solution there. Use getattr
getattr(instance, getattr(self, language + '_field'))
Upvotes: 2