A Django field extension that allows choices to have rich metadata beyond the standard (value, display) tuple
Project description
Django Meta Choices
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.
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
'django_metachoices',
]
Quick Start
from django.db import models
from django_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,
},
"SUSPENDED": {
"display": "Suspended",
"color": "#dc3545",
"description": "User is temporarily suspended",
"icon": "pause-circle",
"priority": 3,
}
}
class User(models.Model):
name = models.CharField(max_length=100)
status = MetaChoiceField(choices=STATUS_CHOICES)
# Usage
user = User.objects.create(name="John", status="ACTIVE")
# Access metadata through dynamic getters
print(user.get_status_display()) # "Active"
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
Requirements
- Python: 3.8+
- Django: 3.2, 4.0, 4.1, 4.2, 5.0
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_metachoices-0.1.0.tar.gz.
File metadata
- Download URL: django_metachoices-0.1.0.tar.gz
- Upload date:
- Size: 13.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a01a11c8ade404aba2120aab419302495ae086f14d1a1ec75515e754da2f69ab
|
|
| MD5 |
d195e9d5ec06a0002fac35ecb8494760
|
|
| BLAKE2b-256 |
caac4fb15cc38a30f37015507d54908a1599fc72f16edae09f773642c736489c
|
File details
Details for the file django_metachoices-0.1.0-py3-none-any.whl.
File metadata
- Download URL: django_metachoices-0.1.0-py3-none-any.whl
- Upload date:
- Size: 12.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a69a92b3e5051488c5d9bc9172673044d5961ccd9ce4ebdc636cc81d64bb6c81
|
|
| MD5 |
233c5517d955cb4514a540d7542464fa
|
|
| BLAKE2b-256 |
5e5909d3a5a3ef700535993d3563fad1ebdd75b8ed5a0179ac01e2c4b8765de9
|