Skip to main content

Utility for Django to bind field choices with rich data.

Project description

django datachoices


License: MIT PyPI - Version

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_datachoices-1.0.0.tar.gz (8.1 kB view details)

Uploaded Source

Built Distribution

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

django_datachoices-1.0.0-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

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

Hashes for django_datachoices-1.0.0.tar.gz
Algorithm Hash digest
SHA256 dd4173ae31200345e7bf81ba56e4ecd61634b70df5597320ad4f22f6163cf805
MD5 f9143c2f0b938073ace8fbdb53b8b3ac
BLAKE2b-256 e734a54ae1cf4ea05fd38a99422f5593fc4e623c60e86440b65a86e720590adf

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_datachoices-1.0.0.tar.gz:

Publisher: release.yml on blu14x/django-datachoices

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file django_datachoices-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_datachoices-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fd97830c2d46d4bd4b3d8ee60f222968e83a46eaaaab934d5fce272a3b973ba7
MD5 c85e4748a8d3460fb913b4e55a16cfcb
BLAKE2b-256 d7b54569af14735fdb6b129d3b5d1ebb971618afd3768b743bc1d2f9586a5a50

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_datachoices-1.0.0-py3-none-any.whl:

Publisher: release.yml on blu14x/django-datachoices

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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