Skip to main content

A Django field extension that allows choices to have rich metadata beyond the standard (value, display) tuple

Project description

Django Meta Choices

PyPI version Python versions Django versions CI codecov License: MIT

A Django field extension that allows choices to have rich metadata beyond the standard (value, display) tuple.

Overview

Django's standard choice fields only support a simple key-value mapping. This package extends that functionality to allow arbitrary metadata for each choice, which can be accessed through dynamically generated getter methods.

Example

Without metachoices

STATUS_CHOICES = {
    "ACTIVE": "Active",
    "INACTIVE": "Inactive",
}

class User(models.Model):
    name = models.CharField(max_length=100)
    status = models.CharField(choices=STATUS_CHOICES) # The normal CharField with choices


# Usage
user = User.objects.create(name="John", status="ACTIVE")

# Access choice data as usual
print(user.status)                    # The database stored value, "ACTIVE"
print(user.get_status_display())      # The human-readable display value, "Active"

With metachoices

from django.db import models
from metachoices import MetaChoiceField

# Define choices with rich metadata
STATUS_CHOICES = {
    "ACTIVE": {
        "display": "Active",
        "color": "#28a745",
        "description": "User is active and can access the system",
        "icon": "check-circle",
        "priority": 1,
    },
    "INACTIVE": {
        "display": "Inactive", 
        "color": "#6c757d",
        "description": "User is inactive and cannot access the system",
        "icon": "x-circle",
        "priority": 2,
    },
}

class User(models.Model):
    name = models.CharField(max_length=100)
    status = MetaChoiceField(choices=STATUS_CHOICES) # Use MetaChoiceField with choices instead of CharField

# Usage
user = User.objects.create(name="John", status="ACTIVE")

# Access choice data as usual
print(user.status)                    # The database stored value, "ACTIVE"
print(user.get_status_display())      # The human-readable display value, "Active"

# With richer capabilities!
print(user.get_status_color())        # "#28a745"
print(user.get_status_description())  # "User is active and can access the system"
print(user.get_status_icon())         # "check-circle"
print(user.get_status_priority())     # 1

Features

  • Rich Metadata: Add any number of attributes to your choices (description, url, icon, priority, etc.)
  • Dynamic Getters: Automatically generates get_<field>_<attribute>() methods for all metadata attributes
  • Django Compatible: Works seamlessly with Django's existing choice field functionality
  • Admin Integration: Fully compatible with Django admin
  • Type Safe: Validates that field values are valid choice keys
  • Auto-Detection: Automatically detects whether to use CharField or IntegerField based on choice keys

Installation

Install from PyPI:

pip install django-metachoices

Add to your Django project's INSTALLED_APPS:

INSTALLED_APPS = [
    # ... other apps
    'metachoices',
]

Requirements

  • Python: 3.13+
  • Django: 5.2+

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

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_metachoices-0.1.4.tar.gz (10.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_metachoices-0.1.4-py3-none-any.whl (10.0 kB view details)

Uploaded Python 3

File details

Details for the file django_metachoices-0.1.4.tar.gz.

File metadata

  • Download URL: django_metachoices-0.1.4.tar.gz
  • Upload date:
  • Size: 10.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.3

File hashes

Hashes for django_metachoices-0.1.4.tar.gz
Algorithm Hash digest
SHA256 3163a964510b37688fe74c1bb897b3d77f69e23f0cf50ae1de5857c897b36526
MD5 7130146d94779ca7a32e07e03c6a61f0
BLAKE2b-256 2afbf648a6b453b51720d7dfc3f614bc55c6ca219a0ce447a1548fc186af0932

See more details on using hashes here.

File details

Details for the file django_metachoices-0.1.4-py3-none-any.whl.

File metadata

File hashes

Hashes for django_metachoices-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 48118cf29557b108c30a08b2b53f0c968754a34630cdd9e1e083ea8480732a2c
MD5 e6972dcdb1aab75687661a5d9c4f094b
BLAKE2b-256 cf08150574a4f0917d19943d2efb013b882a996c8366ba1d541697e1a6195ee5

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