Skip to main content

Django field that set/get django's new TextChoices/IntegerChoices enum.

Project description

django-choices-field

build status coverage PyPI version python version django version

Django field that set/get django's new TextChoices/IntegerChoices enum.

Install

pip install django-choices-field

Usage

import enum

from django.db import models
from django_choices_field import TextChoicesField, IntegerChoicesField, IntegerChoicesFlag


class MyModel(models.Model):
    class TextEnum(models.TextChoices):
        FOO = "foo", "Foo Description"
        BAR = "bar", "Bar Description"

    class IntegerEnum(models.IntegerChoices):
        FIRST = 1, "First Description"
        SECOND = 2, "Second Description"

    class IntegerFlagEnum(IntegerChoicesFlag):
        FIRST = enum.auto(), "First Option"
        SECOND = enum.auto(), "Second Option"
        THIRD = enum.auto(), "Third Option"

    text_field = TextChoicesField(
        choices_enum=TextEnum,
        default=TextEnum.FOO,
    )
    integer_field = IntegerChoicesField(
        choices_enum=IntegerEnum,
        default=IntegerEnum.FIRST,
    )
    flag_field = IntegerChoicesFlagField(
        choices_enum=IntegerFlagEnum,
        default=IntegerFlagEnum.FIRST | IntegerFlagEnum.SECOND,
    )


obj = MyModel()
reveal_type(obj.text_field)  # MyModel.TextEnum.FOO
assert isinstance(obj.text_field, MyModel.TextEnum)
assert obj.text_field == "foo"

reveal_type(obj.integer_field)  # MyModel.IntegerEnum.FIRST
assert isinstance(obj.integer_field, MyModel.IntegerEnum)
assert obj.integer_field == 1

reveal_type(obj.flag_field)  # MyModel.IntegerFlagEnum.FIRST | MyModel.IntegerFlagEnum.SECOND
assert isinstance(obj.integer_field, MyModel.IntegerFlagEnum)
assert obj.flag_field == 3

NOTE: The IntegerChoicesFlag requires python 3.11+ to work properly.

License

This project is licensed under MIT licence (see LICENSE for more info)

Contributing

Make sure to have poetry installed.

Install dependencies with:

poetry install

Run the testsuite with:

poetry run pytest

Feel free to fork the project and send me pull requests with new features, corrections and translations. I'll gladly merge them and release new versions ASAP.

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_choices_field-2.3.0.tar.gz (6.4 kB view details)

Uploaded Source

Built Distribution

django_choices_field-2.3.0-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file django_choices_field-2.3.0.tar.gz.

File metadata

  • Download URL: django_choices_field-2.3.0.tar.gz
  • Upload date:
  • Size: 6.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.12.1 Linux/6.5.0-1015-azure

File hashes

Hashes for django_choices_field-2.3.0.tar.gz
Algorithm Hash digest
SHA256 bb0c85c79737ab98bfb9c0d9ddf98010d612c0585be767890e25fd192c3d1694
MD5 dd51f6ad4350bd68369bdc4abe48138b
BLAKE2b-256 05a480cfa63a1232a74eae7e9e6aedb79582e825e58b3fa0563b51fd89a78a24

See more details on using hashes here.

File details

Details for the file django_choices_field-2.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_choices_field-2.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4bdcccf802bff9065af19798810de494dd16337fa46dae01680ede12a10280d6
MD5 26c660e7da79b8a4c59b5fb533768a65
BLAKE2b-256 ebe0a6e14c493a8d9c0f4be2ed4390b008c723ee05c9e0f8271b4a0fea82b1bd

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page