# django-choices-field
[](https://actions-badge.atrox.dev/bellini666/django-choices-field/goto?ref=master)
[](https://codecov.io/gh/bellini666/django-choices-field)
[](https://pypi.org/project/django-choices-field/)


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/0b/bd/bae7730699a4b75e4621ba9e70c2dd818c5af877c6e7cf3157278689f88c/django_choices_field-3.0.1.tar.gz",
"platform": null,
"description": "# django-choices-field\n\n[](https://actions-badge.atrox.dev/bellini666/django-choices-field/goto?ref=master)\n[](https://codecov.io/gh/bellini666/django-choices-field)\n[](https://pypi.org/project/django-choices-field/)\n\n\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.1",
"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": "bbf878b38bfdfd9d8c40061524346ec3bd100169daaa254ff682d895b55196f6",
"md5": "dd6b4fdad40b55e10e08d99703edd3e4",
"sha256": "3e99b832c6d2af93a7ffaf1b95c197278fef0b35673377bf853f73576510b9ee"
},
"downloads": -1,
"filename": "django_choices_field-3.0.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "dd6b4fdad40b55e10e08d99703edd3e4",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4.0,>=3.8",
"size": 6392,
"upload_time": "2025-08-14T21:36:46",
"upload_time_iso_8601": "2025-08-14T21:36:46.857992Z",
"url": "https://files.pythonhosted.org/packages/bb/f8/78b38bfdfd9d8c40061524346ec3bd100169daaa254ff682d895b55196f6/django_choices_field-3.0.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "0bbdbae7730699a4b75e4621ba9e70c2dd818c5af877c6e7cf3157278689f88c",
"md5": "7f38379b907b752d9e59e208cb82bd87",
"sha256": "5b45f5d3aa2ef3c30a7ff296ac7acc743097fb3185c78f8c4256c6a73854ab20"
},
"downloads": -1,
"filename": "django_choices_field-3.0.1.tar.gz",
"has_sig": false,
"md5_digest": "7f38379b907b752d9e59e208cb82bd87",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4.0,>=3.8",
"size": 5717,
"upload_time": "2025-08-14T21:36:48",
"upload_time_iso_8601": "2025-08-14T21:36:48.033676Z",
"url": "https://files.pythonhosted.org/packages/0b/bd/bae7730699a4b75e4621ba9e70c2dd818c5af877c6e7cf3157278689f88c/django_choices_field-3.0.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-08-14 21:36:48",
"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"
}