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.1.0.tar.gz (8.2 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.1.0-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

File details

Details for the file django_datachoices-1.1.0.tar.gz.

File metadata

  • Download URL: django_datachoices-1.1.0.tar.gz
  • Upload date:
  • Size: 8.2 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.1.0.tar.gz
Algorithm Hash digest
SHA256 30e11c1f6d86712aa6592f192096ee99f46a89fc5c7cd412bb32e640eb0448a7
MD5 2df7a8d96c539e915603041e5e9da824
BLAKE2b-256 96a7d1ecd40db513970653dadbebfb6683770bc7e60a13a208019f3720408b85

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_datachoices-1.1.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.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_datachoices-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0f80fe834680db354c69ae5fa59e48a97ea542bdf33af68df48563676cae6d9f
MD5 56b92a170608f380ef6fc369d6517526
BLAKE2b-256 72c527868983b889508592a754022e71c333b2f9a4d502c6850a021a8af5a200

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_datachoices-1.1.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