Skip to main content

Sanity for the django choices functionality.

Project description

Documentation is a work in progress.

Django choices provides a declarative way of using the choices option on django fields. More information on its development can be found at its Home Page.

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
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)

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


Download files

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

Source Distribution

django-choices-1.1.8.tar.gz (3.9 kB view details)

Uploaded Source

File details

Details for the file django-choices-1.1.8.tar.gz.

File metadata

File hashes

Hashes for django-choices-1.1.8.tar.gz
Algorithm Hash digest
SHA256 a16bdad1d86651100977119349cb7bc758f8c58a1200cd651a97cdefb33c39f8
MD5 49a98c9562fdb954aa5a0e930a710a27
BLAKE2b-256 a7af4bdd150e8f7c8d42777353e8f39550cf2f82b42a012df7aef266c8173adb

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page