Registry / web-framework / django-choices

django-choices

JSON →
library2.0.0pypypi✓ verified 24d ago

Django-choices provides a declarative way of using the `choices` option on Django fields, aiming to add order and sanity to Django model choices. The current version is 2.0.0. While it served a valuable purpose, the library is now in maintenance mode, with its maintainers strongly recommending migration to native Django 3.0+ `TextChoices` or `IntegerChoices` for new projects due to Django's improved built-in functionality.

pip install django-choices
INSTALL
IMPORT
SIG · DJANGO-CHOICES
D
django-choices
web-frameworkpythonv2.0.0
Install
3.5s avg
Import
857ms
Disk
66MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v2.0.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.560s · 67MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 3.5s · import 0.468s · 68MB
66MB installed
● package 66MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

DjangoChoices
from djchoices import DjangoChoices
from djchoices import DjangoChoices

Define choice sets using `DjangoChoices` and `ChoiceItem` classes, then integrate them into Django model fields. The `choices` attribute of your `DjangoChoices` class provides the iterable required by Django model fields.

import os import django from django.conf import settings from django.db import models # Minimal Django settings for standalone script settings.configure( INSTALLED_APPS=['test_app'], DATABASES={'default': {'ENGINE': 'django.db.backends.sqlite3', 'NAME': ':memory:'}}, USE_I18N=True, # Required for ugettext_lazy if used in ChoiceItem ) django.setup() from djchoices import ChoiceItem, DjangoChoices class Author(models.Model): name = models.CharField(max_length=100) def __str__(self): return self.name class Book(models.Model): class BookType(DjangoChoices): short_story = ChoiceItem('short', 'Short story') novel = ChoiceItem('novel', 'Novel') non_fiction = ChoiceItem('non_fiction', 'Non fiction') author = models.ForeignKey(Author, on_delete=models.CASCADE) book_type = models.CharField( max_length=20, choices=BookType.choices, default=BookType.novel ) def __str__(self): return f"{self.book_type.label} by {self.author.name}" # Example usage (requires an actual Django project setup normally) if __name__ == '__main__': # This part would typically be in your Django ORM interaction code # For this example, we'll just demonstrate the choice access. print(f"Book type choices: {Book.BookType.choices}") print(f"Default book type value: {Book.BookType.novel.value}") print(f"Default book type label: {Book.BookType.novel.label}") # In a real Django setup, you would create and query model instances. # Example of accessing choices directly: status_options = [] for value, label in Book.BookType.choices: status_options.append(f"Value: {value}, Label: {label}") print("\nAvailable Book Types:") for option in status_options: print(option)
Debug
Known issues
deprecatedThe `django-choices` library is no longer actively developed, and its use is strongly discouraged for new projects. Django 3.0+ introduced native enumeration types (`TextChoices`, `IntegerChoices`) that largely supersede the functionality provided by this package.
fix
For new projects or when refactoring, use `django.db.models.TextChoices` or `django.db.models.IntegerChoices`. Consult Django's official documentation for usage patterns.
affects: 2.0.0 and earlier
gotchaThere are several similarly named packages (e.g., `django-choices-field`, `django-js-choices`) that serve different purposes. Ensure you are installing and importing from the correct `django-choices` library by `bigjason`.
fix
Always verify the PyPI project page and GitHub repository (`https://github.com/bigjason/django-choices`) to confirm you're using the intended library. The correct import path is `from djchoices import ...`.
affects: All versions
gotchaThe module to import from is `djchoices`, not `django_choices` or `django.choices`. This is a common point of confusion.
fix
Ensure your import statements use `from djchoices import ChoiceItem, DjangoChoices`.
affects: All versions
Upgrade
Version history
2.0.0latest on PyPI · released Jul 24, 2023
Audit
Dependencies
DjangorequiredCore framework dependency; tested against Django 3.2+.
Agent activity
9 hits · last 30 days
node
6
Resources
django-choices — pip install django-choices · libregistry