Order and sanity for django model choices.
Django choices is fairly simple, so most Python and Django versions should work. It is tested against Python 2.7, 3.3, 3.4, 3.5 and PyPy. Django 1.8 until and including 1.11 alpha are supported (and tested in Travis).
If you need to support older Python or Django versions, you should stick with
1.4.4. Backwards compatibility is dropped from 1.5 onwards.
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 ChoiceItem, DjangoChoices class Book(models.Model): class BookType(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=BookType.choices, default=BookType.novel )
You can then use the availe choices in other modules, e.g.:
from .models import Book Person.objects.create(author=my_author, type=Book.BookTypes.short_story)
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.