Django enumeration for model field choices

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
"""
>>> states = Enum('OPEN', 'CLOSED')
>>> states.OPEN
0
>>> states.get_choices()
((0, 'OPEN'), (1, 'CLOSED'))
"""
class DjangoEnum(object):
    def __init__(self, *string_list):
        self.__dict__.update([(string, number) for (number, string)
                              in enumerate(string_list)])

    def get_choices(self):
        return tuple(enumerate(self.__dict__.keys())) 

More like this

  1. Enumeration field by nail.xx 4 years, 10 months ago
  2. Better Django Model Field Choices by jorjun 2 years, 2 months ago
  3. Storing IP address ModelField as Integer, without nasty hacks.. by sleepycal 1 year, 10 months ago
  4. More readable Enumeration class for Django choices by achimnol 3 years, 10 months ago
  5. Choices helper by jacobian 3 years, 9 months ago

Comments

(Forgotten your password?)