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. Read the full documentation on ReadTheDocs.
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)
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"
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.5.0-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fcefb1ba8c53c0d0a0452e1c637656a7e92c5c6ccbddcb969ed9a82e50b12184 |
|
MD5 | 9293a9e5765c5110a0e395df5d38d40c |
|
BLAKE2b-256 | 39243b37d34df8934063fe58e9e7d9394ca5fa8a1fc21446345ae1ad64a80fa0 |