Order and sanity for django model choices.
Django choices is fairly simple, so most Python and Django versions should work. Django choices is tested against Python 2.6, 2.7, 3.3, 3.4, 3.5 and PyPy. Django 1.3 until 1.9 (including) are officially supported (= tested in Travis).
Install like any other library:
pip install django-choices
There is no need to add it in your installed apps.
To use it, you write a choices class, and use it in your model fields:
from djchoices import DjangoChoices, ChoiceItem class Book(models.Model): class BookTypes(DjangoChoices): short_story = ChoiceItem('short', 'Short story') novel = ChoiceItem('novel', 'Novel') non_fiction = ChoiceItem('non_fiction', 'Non fiction') author = models.ForeignKey('Author') book_type = models.CharField( max_length=20, choices=BookTypes.choices, default=BookTypes.novel, validators=[BookTypes.validator] )
You can use this in other places like this:
from .models import Book Person.objects.create(author=my_author, type=Book.BookTypes.short_story)
The DjangoChoices classes can be located anywhere you want, for example you can put them outside of the model declaration if you have a ‘common’ set of choices for different models. Any place is valid though, you can group them all together in choices.py if you want.