Skip to content

A handy class for creating "choices" objects, primarily for use in Django

License

Notifications You must be signed in to change notification settings

Kazade/django-choices

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

django-choices

A handy class for creating "choices" objects, primarily for use in Django

Usage

Essentially you just wrap a normal iterable of (value, label) pairs in the Choices class.

my_choices = Choices([
    ('BLUE', 'Blue'),
    ('BANANA', 'Banana'),
    ('THING', 'Some Thing'),
])

You then have access to:

  • my_choices.choices - this gives an iterable of pairs, as you would pass to a Django model field.
  • my_choices.constants - this gives an iterable of the values only.
  • my_choices.THING - this is a reference to this particular choice, useful for defining things by reference rather than hard coding the same string literal in multiple places.

Example

from choices import Choices

MY_CHOICES = Choices([
    ('YES', 'Yes'),
    ('NO', 'No'),
    ('MAYBE', 'Maybe')
])

class MyModel(models.Model):

    decision = models.CharField(
        choices=MY_CHOICES.choices,
        default=MY_CHOICES.YES
    )

About

A handy class for creating "choices" objects, primarily for use in Django

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 100.0%