Sanity for the django choices functionality.
Project description
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for django-choices-1.4.4.linux-x86_64.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | a60a0f10d6a6dff475d69f6dea918f13a889187707491218e6c3920c10b3ade9 |
|
MD5 | cfddaf63cca0b70a187fd126b81fa168 |
|
BLAKE2b-256 | 45577ba6497e2fbe5c56224a87ca08344d646f4f0ea5442321b472edfdcb21c2 |