Sanity to the django choices functionality.
Python
Latest commit ee1be5f Sep 27, 2016 @dokterbob dokterbob committed on GitHub Merge pull request #41 from sobolevn/patch-1
Updated `README.rst` with svg bagde
Permalink
Failed to load latest commit information.
djchoices Fixed #33: access dict methods of Labels (IPython support) Aug 28, 2016
docs
.coveragerc Move to tox for unit testing Sep 10, 2015
.gitignore Ignore build directory Jun 25, 2016
.travis.yml Add Django 1.10 to the testing matrix Jun 1, 2016
CONTRIBUTING.md Added some contributing guidelines. May 31, 2016
CONTRIBUTORS.md Give credit to contributors May 31, 2016
Changelog.rst Update changelog Jun 1, 2016
LICENSE PEP8 cleanup Aug 29, 2015
MANIFEST.in Added README.rst to the distribution. Feb 28, 2011
README.rst Updated `README.rst` with svg bagde Sep 27, 2016
runtests.py Move to tox for unit testing Sep 10, 2015
setup.cfg Add wheel config Jun 25, 2016
setup.py Bump version Aug 28, 2016
tox.ini Django 1.10 instead of 1.10 alpha Aug 28, 2016

README.rst

Django-Choices

https://secure.travis-ci.org/bigjason/django-choices.svg?branch=master https://coveralls.io/repos/bigjason/django-choices/badge.svg?branch=master&service=github Documentation Status

Order and sanity for django model choices.

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

Installation

You can install via PyPi or direct from the github repo.

To install with pip:

$ pip install django-choices

To install with easy_install:

$ easy_install django-choices

Basic Usage

To start you create a choices class. Then you point the choices property on your fields to the choices attribute of the new class. Django will be able to use the choices and you will be able to access the values by name. For example you can replace this:

# In models.py
class Person(models.Model):
    # Choices
    PERSON_TYPE = (
        ("C", "Customer"),
        ("E", "Employee"),
        ("G", "Groundhog"),
    )
    # Fields
    name = models.CharField(max_length=32)
    type = models.CharField(max_length=1, choices=PERSON_TYPE)

With this:

# In models.py
from djchoices import DjangoChoices, ChoiceItem

class Person(models.Model):
    # Choices
    class PersonType(DjangoChoices):
        Customer = ChoiceItem("C")
        Employee = ChoiceItem("E")
        Groundhog = ChoiceItem("G")

    # Fields
    name = models.CharField(max_length=32)
    type = models.CharField(max_length=1,
                            choices=PersonType.choices,
                            validators=[PersonType.validator])

You can use this elsewhere like this:

# Other code
Person.create(name="Phil", type=Person.PersonType.Groundhog)

You can use without value, and the label will be used as value:

class Sample(DjangoChoices):
    OptionA = ChoiceItem()
    OptionB = ChoiceItem()

print(Sample.OptionA) # "OptionA"

The DjangoChoices classes can be located anywhere you want. If I have a lot of declarations I will sometimes place them in a const.py or choices.py.

License

Licensed under the MIT License.

Souce Code

The source code can be found on github.