just so you know, there are some enumeration tools in Python. Just not
an 'enum' type:
>>> base_choices = ['No', 'Yes'] <-- transform this any way you want:
>>> choices = list(enumerate(base_choices))
>>> choices
[(0, 'No'), (1, 'Yes')] <-- can be used for 'choices' parameter in
BooleanField
>>> use_txt_choices = {v:k for k,v in enumerate(base_choices)}
>>> use_txt_choices
{'Yes': 1, 'No': 0} <-- can be used in 'clean' method:
if len(txt) == 0 and use_txt==use_txt_choices['Yes']:
pass
If the values have to be boolean instead of ints, you can:
>>> txt_choices = [(True if i else False, j) for i,j in enumerate(base_choices)]
>>> txt_choices
[(False, 'No'), (True, 'Yes')]
>>> lookup_choices = {v:True if k else False for k,v in enumerate(base_choices)}
>>> lookup_choices
{'Yes': True, 'No': False}
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
No comments:
Post a Comment