# 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": "",
"docs_url": null,
"requires_python": ">=3.8,<4.0",
"maintainer_email": "",
"keywords": "django,enum",
"author": "Thiago Bellini Ribeiro",
"author_email": "thiago@bellini.dev",
"download_url": "https://files.pythonhosted.org/packages/05/a4/80cfa63a1232a74eae7e9e6aedb79582e825e58b3fa0563b51fd89a78a24/django_choices_field-2.3.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": "2.3.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": "ebe0a6e14c493a8d9c0f4be2ed4390b008c723ee05c9e0f8271b4a0fea82b1bd",
"md5": "26c660e7da79b8a4c59b5fb533768a65",
"sha256": "4bdcccf802bff9065af19798810de494dd16337fa46dae01680ede12a10280d6"
},
"downloads": -1,
"filename": "django_choices_field-2.3.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "26c660e7da79b8a4c59b5fb533768a65",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8,<4.0",
"size": 6172,
"upload_time": "2024-03-08T19:14:34",
"upload_time_iso_8601": "2024-03-08T19:14:34.973794Z",
"url": "https://files.pythonhosted.org/packages/eb/e0/a6e14c493a8d9c0f4be2ed4390b008c723ee05c9e0f8271b4a0fea82b1bd/django_choices_field-2.3.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "05a480cfa63a1232a74eae7e9e6aedb79582e825e58b3fa0563b51fd89a78a24",
"md5": "dd51f6ad4350bd68369bdc4abe48138b",
"sha256": "bb0c85c79737ab98bfb9c0d9ddf98010d612c0585be767890e25fd192c3d1694"
},
"downloads": -1,
"filename": "django_choices_field-2.3.0.tar.gz",
"has_sig": false,
"md5_digest": "dd51f6ad4350bd68369bdc4abe48138b",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8,<4.0",
"size": 6448,
"upload_time": "2024-03-08T19:14:37",
"upload_time_iso_8601": "2024-03-08T19:14:37.147945Z",
"url": "https://files.pythonhosted.org/packages/05/a4/80cfa63a1232a74eae7e9e6aedb79582e825e58b3fa0563b51fd89a78a24/django_choices_field-2.3.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-03-08 19:14:37",
"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"
}