Skip to main content

Sanity for the django choices functionality.

Project description

Build status Code quality checks Code coverage Documentation Status black pypi python-versions django-versions

Order and sanity for django model choices.

Django choices provides a declarative way of using the choices option on django fields. Read the full documentation on ReadTheDocs.

Note: Django 3.0 added enumeration types. This feature mostly replaces the need for Django-Choices. See also Adam Johnson’s post on using them.


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

$ pip 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
class Person(models.Model):
    # Choices
        ("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
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)

You can use this elsewhere like this:

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

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

class Sample(DjangoChoices):
    option_a = ChoiceItem()
    option_b = ChoiceItem()

print(Sample.option_a)  # "option_a"


Licensed under the MIT License.

Source Code

The source code can be found on github.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for django-choices, version 1.7.2
Filename, size File type Python version Upload date Hashes
Filename, size django_choices-1.7.2-py2.py3-none-any.whl (8.7 kB) File type Wheel Python version py2.py3 Upload date Hashes View
Filename, size django-choices-1.7.2.tar.gz (8.1 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page