Skip to main content

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.

Release files for django-choices-field 4.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-choices-field 4.0.0
File Size Uploaded
django_choices_field-4.0.0.tar.gz 6.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-choices-field 4.0.0
File Interpreter ABI Platform
django_choices_field-4.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 13.4 kB

Release files / django_choices_field-4.0.0.tar.gz

Download URL django_choices_field-4.0.0.tar.gz
Size 6.3 kB
Tags Source
SHA-256 checksum
How to use checksums
7b07aacb2ad2b3ee3c8be78be98302ebbcb606285d20547a4163287819364954
BLAKE2b-256 checksum
How to use checksums
3e2354749b86f605aafeaab81dc8e023968d95a7cc44434eac4df330e8e7f0d8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.2.1 CPython/3.12.1 Linux/6.11.0-1018-azure

Release files / django_choices_field-4.0.0-py3-none-any.whl

Download URL django_choices_field-4.0.0-py3-none-any.whl
Size 7.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
dfa59033685af989f5bc49d95a586e8de692999d3179bf1cb14ef99879e6ff45
BLAKE2b-256 checksum
How to use checksums
3dfd2e0bb952961ba108bdb82efbb10eaeae5bbb5a687b6bdaf8427af181ac5d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.2.1 CPython/3.12.1 Linux/6.11.0-1018-azure

Release history Release notifications | RSS feed

This release

4.0.0 This release

2 release files

3.1.1

2 release files

3.1.0

2 release files

3.0.1

2 release files

3.0.0

2 release files

2.3.0

2 release files

2.2.2

2 release files

2.2.1

2 release files

2.2.0

2 release files

2.1.2

2 release files

2.1.1

2 release files

2.1

2 release files

2.0

2 release files

1.3

2 release files

1.2

2 release files

1.1

2 release files

1.0

2 release files

0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page