Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
123 lines (73 sloc) 3.03 KB

Django-Choices

Order and sanity for django model choices.

https://travis-ci.org/bigjason/django-choices.png

Contents:

Overview

Django choices provides a declarative way of using the choices option on django fields.

Requirements

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 version 1.4.4. Backwards compatibility is dropped from 1.5 onwards.

Quick-start

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)

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.

License

Licensed under the MIT License.

Souce Code and contributing

The source code can be found on github.

Bugs can also be reported on the github repository, and pull requests are welcome. See :ref:`contributing` for more details.

Indices and tables