choices choice utility

16 views
Skip to first unread message

Mike Dewhirst

unread,
May 5, 2017, 3:05:58 AM5/5/17
to Django users
Because I use many of the same constants in different apps in my project
I keep most of them including almost all choices in a single __init__.py
file and import them wherever required.

Here is a tiny utility I use all the time ... whenever
get_<fieldname>_display() is inappropriate.


def choose(choices, choice):
for pair in choices:
if pair[0] == choice:
return pair[1]
for section in choices:
for pair in section[1]:
if pair[0] == choice:
return pair[1]


Hope someone finds it useful. Also, I'm sure it can be made much more
pythonic. It is Friday after all :)

Cheers

Mike




Reply all
Reply to author
Forward
0 new messages