Skip to main content

Sanity for the django choices functionality.

Project description

https://secure.travis-ci.org/bigjason/django-choices.png?branch=master

Order and sanity for django model choices.

Documentation is a work in progress.

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)

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.

Project details


Release history Release notifications

History Node

1.6.0

History Node

1.5.1

History Node

1.5.0

History Node

1.4.4

History Node

1.4.3

History Node

1.4.2

History Node

1.4

History Node

1.3

This version
History Node

1.2

History Node

1.1.12

History Node

1.1.11

History Node

1.1.9

History Node

1.1.8

History Node

1.1.7

History Node

1.1.6

History Node

1.1.2

History Node

1.1.1

History Node

1.1.0

History Node

1.0.5

History Node

1.0.0

Download files

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

Filename, size & hash SHA256 hash help File type Python version Upload date
django-choices-1.2.tar.gz (4.4 kB) Copy SHA256 hash SHA256 Source None Jul 2, 2014

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging CloudAMQP CloudAMQP RabbitMQ AWS AWS Cloud computing Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page