[Fixed]-Django model field choices – wouldn't a dict be better?

15👍

I believe the keys of a dict are not guaranteed to be sorted (unless you use OrderedDict obviously). That is, you “might” get “Offline”, “Dev”, “Live” choices with your version.

Implementation note on dict.items:

Keys and values are listed in an arbitrary order which is non-random, varies across Python implementations, and depends on the dictionary’s history of insertions and deletions.

3👍

Expanding on my comment on @vicvicvic’s answer:

t = [ 'Live', 'Offline', 'Dev', ]
status = models.SmallIntegerField( choices=[(i,t[i]) for i in range(len(t))] )
...
if not 0 <= int(input1) < len(t): print "invalid"
if input2 not in t: print "invalid"
status = t.index(input2)
👤Jake

Leave a comment