django-choices-field


Namedjango-choices-field JSON
Version 3.0.0 PyPI version JSON
download
home_pagehttps://github.com/bellini666/django-choices-field
SummaryDjango field that set/get django's new TextChoices/IntegerChoices enum.
upload_time2025-08-10 12:34:59
maintainerNone
docs_urlNone
authorThiago Bellini Ribeiro
requires_python<4.0,>=3.8
licenseMIT
keywords django enum
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage
            # django-choices-field

[![build status](https://img.shields.io/endpoint.svg?url=https%3A%2F%2Factions-badge.atrox.dev%2Fbellini666%2Fdjango-choices-field%2Fbadge%3Fref%3Dmaster&style=flat)](https://actions-badge.atrox.dev/bellini666/django-choices-field/goto?ref=master)
[![coverage](https://img.shields.io/codecov/c/github/bellini666/django-choices-field.svg)](https://codecov.io/gh/bellini666/django-choices-field)
[![PyPI version](https://img.shields.io/pypi/v/django-choices-field.svg)](https://pypi.org/project/django-choices-field/)
![python version](https://img.shields.io/pypi/pyversions/django-choices-field.svg)
![django version](https://img.shields.io/pypi/djversions/django-choices-field.svg)

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

## Install

```bash
pip install django-choices-field
```

## Usage

```python
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](https://python-poetry.org/) installed.

Install dependencies with:

```bash
poetry install
```

Run the testsuite with:

```bash
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.

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/bellini666/django-choices-field",
    "name": "django-choices-field",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<4.0,>=3.8",
    "maintainer_email": null,
    "keywords": "django, enum",
    "author": "Thiago Bellini Ribeiro",
    "author_email": "thiago@bellini.dev",
    "download_url": "https://files.pythonhosted.org/packages/bb/82/97603c2e36e6cd698bb6ff5d091e1f38f20eaa35e5350223ce8ebdc140d3/django_choices_field-3.0.0.tar.gz",
    "platform": null,
    "description": "# django-choices-field\n\n[![build status](https://img.shields.io/endpoint.svg?url=https%3A%2F%2Factions-badge.atrox.dev%2Fbellini666%2Fdjango-choices-field%2Fbadge%3Fref%3Dmaster&style=flat)](https://actions-badge.atrox.dev/bellini666/django-choices-field/goto?ref=master)\n[![coverage](https://img.shields.io/codecov/c/github/bellini666/django-choices-field.svg)](https://codecov.io/gh/bellini666/django-choices-field)\n[![PyPI version](https://img.shields.io/pypi/v/django-choices-field.svg)](https://pypi.org/project/django-choices-field/)\n![python version](https://img.shields.io/pypi/pyversions/django-choices-field.svg)\n![django version](https://img.shields.io/pypi/djversions/django-choices-field.svg)\n\nDjango field that set/get django's new TextChoices/IntegerChoices enum.\n\n## Install\n\n```bash\npip install django-choices-field\n```\n\n## Usage\n\n```python\nimport enum\n\nfrom django.db import models\nfrom django_choices_field import TextChoicesField, IntegerChoicesField, IntegerChoicesFlag\n\n\nclass MyModel(models.Model):\n    class TextEnum(models.TextChoices):\n        FOO = \"foo\", \"Foo Description\"\n        BAR = \"bar\", \"Bar Description\"\n\n    class IntegerEnum(models.IntegerChoices):\n        FIRST = 1, \"First Description\"\n        SECOND = 2, \"Second Description\"\n\n    class IntegerFlagEnum(IntegerChoicesFlag):\n        FIRST = enum.auto(), \"First Option\"\n        SECOND = enum.auto(), \"Second Option\"\n        THIRD = enum.auto(), \"Third Option\"\n\n    text_field = TextChoicesField(\n        choices_enum=TextEnum,\n        default=TextEnum.FOO,\n    )\n    integer_field = IntegerChoicesField(\n        choices_enum=IntegerEnum,\n        default=IntegerEnum.FIRST,\n    )\n    flag_field = IntegerChoicesFlagField(\n        choices_enum=IntegerFlagEnum,\n        default=IntegerFlagEnum.FIRST | IntegerFlagEnum.SECOND,\n    )\n\n\nobj = MyModel()\nreveal_type(obj.text_field)  # MyModel.TextEnum.FOO\nassert isinstance(obj.text_field, MyModel.TextEnum)\nassert obj.text_field == \"foo\"\n\nreveal_type(obj.integer_field)  # MyModel.IntegerEnum.FIRST\nassert isinstance(obj.integer_field, MyModel.IntegerEnum)\nassert obj.integer_field == 1\n\nreveal_type(obj.flag_field)  # MyModel.IntegerFlagEnum.FIRST | MyModel.IntegerFlagEnum.SECOND\nassert isinstance(obj.integer_field, MyModel.IntegerFlagEnum)\nassert obj.flag_field == 3\n```\n\nNOTE: The `IntegerChoicesFlag` requires python 3.11+ to work properly.\n\n## License\n\nThis project is licensed under MIT licence (see `LICENSE` for more info)\n\n## Contributing\n\nMake sure to have [poetry](https://python-poetry.org/) installed.\n\nInstall dependencies with:\n\n```bash\npoetry install\n```\n\nRun the testsuite with:\n\n```bash\npoetry run pytest\n```\n\nFeel free to fork the project and send me pull requests with new features,\ncorrections and translations. I'll gladly merge them and release new versions\nASAP.\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Django field that set/get django's new TextChoices/IntegerChoices enum.",
    "version": "3.0.0",
    "project_urls": {
        "Documentation": "https://django-choices-field.readthedocs.io",
        "Homepage": "https://github.com/bellini666/django-choices-field",
        "Repository": "https://github.com/bellini666/django-choices-field"
    },
    "split_keywords": [
        "django",
        " enum"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "00e58016bd5f1fc0151cb30eeb03de4e096df7081e0188ec253e9ea5cbdbe553",
                "md5": "2db0f8659156ca837c40f5ef3b19f30c",
                "sha256": "b6b17bd795abcd94a0427365c990940955bae3cef6b09710492c3263349f7af5"
            },
            "downloads": -1,
            "filename": "django_choices_field-3.0.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "2db0f8659156ca837c40f5ef3b19f30c",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4.0,>=3.8",
            "size": 6361,
            "upload_time": "2025-08-10T12:34:58",
            "upload_time_iso_8601": "2025-08-10T12:34:58.048280Z",
            "url": "https://files.pythonhosted.org/packages/00/e5/8016bd5f1fc0151cb30eeb03de4e096df7081e0188ec253e9ea5cbdbe553/django_choices_field-3.0.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "bb8297603c2e36e6cd698bb6ff5d091e1f38f20eaa35e5350223ce8ebdc140d3",
                "md5": "0d78f8f5afa1608cee1e530572a35267",
                "sha256": "56e85a5f4ae1ca386b01bc78bbb0c3ec325c59fc16239c81b3339077976d8319"
            },
            "downloads": -1,
            "filename": "django_choices_field-3.0.0.tar.gz",
            "has_sig": false,
            "md5_digest": "0d78f8f5afa1608cee1e530572a35267",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4.0,>=3.8",
            "size": 5682,
            "upload_time": "2025-08-10T12:34:59",
            "upload_time_iso_8601": "2025-08-10T12:34:59.273405Z",
            "url": "https://files.pythonhosted.org/packages/bb/82/97603c2e36e6cd698bb6ff5d091e1f38f20eaa35e5350223ce8ebdc140d3/django_choices_field-3.0.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-08-10 12:34:59",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "bellini666",
    "github_project": "django-choices-field",
    "travis_ci": false,
    "coveralls": true,
    "github_actions": true,
    "lcname": "django-choices-field"
}
        
Elapsed time: 2.37069s