Utility for Django to bind field choices with rich data.
Project description
django datachoices
Utility for Django to bind field choices with rich data.
Provides two core classes — DataChoices and DataChoiceField — designed to simplify handling model field choices that are tied to complex data.
Install
pip install django-datachoices
Usage
from datachoices import DataChoices, DataChoiceField
from django.db import models
class GameChoices(DataChoices, label="title"):
SM64 = {"title": "Super Mario 64", "id": "NAAE", "year": 1996}
SMS = {"title": "Super Mario Sunshine", "id": "GMSP01", "year": 2002}
SMG = {"title": "Super Mario Galaxy", "id": "RMGP01", "year": 2007}
class MyModel(models.Model):
game = DataChoiceField(choices=GameChoices)
> instance = MyModel.objects.create(game=GameChoices.SMS)
> instance.game
"SMS"
> instance.get_game_display()
"Super Mario Sunshine"
> instance.get_game_data()
{"title": "Super Mario Sunshine", "id": "GMSP01", "year": 2002}
label / value attribute
The label (readable representation, e.g., on select widgets) and the value (the actual field value for the database) attributes are set using class arguments:
class GameChoices(DataChoices, label="title", value="id"):
…
> instance.game
"GMSP01"
If not specified, the member name (SM64, SMS or SMG in this example) is used as the value. This is fine for most cases.
However, the label is mostly more interesting for display purposes. If not specified, this defaults to __str__, __name__ or the member name (depending on the type).
from datachoices import DataChoices, DataChoiceField
from django.db import models
from my_app import FooClass, BarClass
class HandlerClassChoices(DataChoices):
FOO = FooClass
BAR = BarClass
class MyModel(models.Model):
handler_class = DataChoiceField(choices=HandlerClassChoices)
> instance = MyModel.objects.create(handler_class=HandlerClassChoices.BAR)
> instance.get_handler_class_display()
"BarClass"
Member types
This package is currently tested using dictionaries, classes, class instances and @dataclass instances as member types.
# dicts
class DictChoices(DataChoices):
FOO = {"title": "Foo", "number": 1}
BAR = {"title": "Bar", "number": 2}
# classes
class ClassChoices(DataChoices):
FOO = FooClass
BAR = BarClass
# class instances
class InstanceChoices(DataChoices):
FOO = SomeClass("Foo")
BAR = SomeClass("Bar")
# instances using dataclass mixin syntax
@dataclass
class SomeMixin:
title: str
number: int
class DataclassInstanceChoices(SomeMixin, DataChoices):
FOO = "Foo", 1
BAR = "Bar", 2
Multiple choice fields
This package also provides a DataChoiceArrayField class that can be used to create multiple choice fields.
Just be aware, this uses django's django.contrib.postgres.fields.ArrayField. So to use it, you need a postgres database.
from datachoices import DataChoices, DataChoiceArrayField
from django.db import models
class GameChoices(DataChoices, label="title"):
SM64 = {"title": "Super Mario 64", "id": "NAAE", "year": 1996}
SMS = {"title": "Super Mario Sunshine", "id": "GMSP01", "year": 2002}
SMG = {"title": "Super Mario Galaxy", "id": "RMGP01", "year": 2007}
class MyModel(models.Model):
games = DataChoiceArrayField(choices=GameChoices)
> instance = MyModel.objects.create(games=[GameChoices.SM64, GameChoices.SMG])
> instance.get_games_display()
"Super Mario 64 & Super Mario Galaxy"
> instance.get_games_data()
[
{"title": "Super Mario 64", "id": "NAAE", "year": 1996},
{"title": "Super Mario Galaxy", "id": "RMGP01", "year": 2007}
]
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
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_datachoices-1.0.0.tar.gz.
File metadata
- Download URL: django_datachoices-1.0.0.tar.gz
- Upload date:
- Size: 8.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dd4173ae31200345e7bf81ba56e4ecd61634b70df5597320ad4f22f6163cf805
|
|
| MD5 |
f9143c2f0b938073ace8fbdb53b8b3ac
|
|
| BLAKE2b-256 |
e734a54ae1cf4ea05fd38a99422f5593fc4e623c60e86440b65a86e720590adf
|
Provenance
The following attestation bundles were made for django_datachoices-1.0.0.tar.gz:
Publisher:
release.yml on blu14x/django-datachoices
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_datachoices-1.0.0.tar.gz -
Subject digest:
dd4173ae31200345e7bf81ba56e4ecd61634b70df5597320ad4f22f6163cf805 - Sigstore transparency entry: 644287050
- Sigstore integration time:
-
Permalink:
blu14x/django-datachoices@4eb48a7b8e1fbf3f959171f497afc2fb88c4b5bb -
Branch / Tag:
refs/heads/master - Owner: https://github.com/blu14x
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@4eb48a7b8e1fbf3f959171f497afc2fb88c4b5bb -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file django_datachoices-1.0.0-py3-none-any.whl.
File metadata
- Download URL: django_datachoices-1.0.0-py3-none-any.whl
- Upload date:
- Size: 6.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fd97830c2d46d4bd4b3d8ee60f222968e83a46eaaaab934d5fce272a3b973ba7
|
|
| MD5 |
c85e4748a8d3460fb913b4e55a16cfcb
|
|
| BLAKE2b-256 |
d7b54569af14735fdb6b129d3b5d1ebb971618afd3768b743bc1d2f9586a5a50
|
Provenance
The following attestation bundles were made for django_datachoices-1.0.0-py3-none-any.whl:
Publisher:
release.yml on blu14x/django-datachoices
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_datachoices-1.0.0-py3-none-any.whl -
Subject digest:
fd97830c2d46d4bd4b3d8ee60f222968e83a46eaaaab934d5fce272a3b973ba7 - Sigstore transparency entry: 644287055
- Sigstore integration time:
-
Permalink:
blu14x/django-datachoices@4eb48a7b8e1fbf3f959171f497afc2fb88c4b5bb -
Branch / Tag:
refs/heads/master - Owner: https://github.com/blu14x
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@4eb48a7b8e1fbf3f959171f497afc2fb88c4b5bb -
Trigger Event:
workflow_dispatch
-
Statement type: