{"id":23562,"library":"django-choices-field","title":"Django Choices Field","description":"A Django field that directly sets/gets Django's TextChoices/IntegerChoices enum values, eliminating manual conversion between enum members and database values. Version 4.0.0 requires Python >=3.10, Django <4.0, and drops support for Python 3.8/3.9. Released irregularly, with breaking changes in v4.0.0 including strict blank/null validation and type checking utilities.","status":"active","version":"4.0.0","language":"python","source_language":"en","source_url":"https://github.com/bellini666/django-choices-field","tags":["django","choices","enum","textchoices","integerchoices","field"],"install":[{"cmd":"pip install django-choices-field","lang":"bash","label":"Install from PyPI"}],"dependencies":[{"reason":"Core dependency for Django fields framework","package":"django","optional":false}],"imports":[{"note":"","symbol":"ChoicesField","correct":"from choices_field import ChoicesField"},{"note":"","symbol":"TypedChoiceFieldMixin","correct":"from choices_field import TypedChoiceFieldMixin"}],"quickstart":{"code":"from django.db import models\nfrom django.utils.translation import gettext_lazy as _\nfrom choices_field import ChoicesField\n\nclass MyModel(models.Model):\n    class Status(models.TextChoices):\n        DRAFT = 'DRAFT', _('Draft')\n        PUBLISHED = 'PUBLISHED', _('Published')\n\n    status = ChoicesField(choices_enum=Status, default=Status.DRAFT)\n\nobj = MyModel(status=MyModel.Status.PUBLISHED)\nprint(obj.status)  # MyModel.Status.PUBLISHED\nprint(obj.status.value)  # 'PUBLISHED'","lang":"python","description":"Define a model with a ChoicesField using Django's TextChoices enum."},"warnings":[{"fix":"Always set null=True when blank=True, or omit blank=True if not needed.","message":"In v4.0.0, passing blank=True without null=True raises ImproperlyConfigured. Previously it was allowed but could cause data integrity issues.","severity":"breaking","affected_versions":">=4.0.0"},{"fix":"Define a ChoicesEnum subclass and pass it to the choices_enum parameter.","message":"Passing choices as a plain list/tuple in the field constructor is deprecated and will be removed in a future version. Use choices_enum instead.","severity":"deprecated","affected_versions":">=4.0.0"},{"fix":"Upgrade to >=3.1.1, or manually override get_FIELD_display() if stuck on older version.","message":"The get_FIELD_display() method works out of the box, but in versions prior to 3.1.1 it was broken for enum fields (returned value instead of label).","severity":"gotcha","affected_versions":"<3.1.1"},{"fix":"Provide a default argument (e.g., default=MyEnum.FOO) or set null=True and/or blank=True.","message":"ChoicesField does not support null=False and blank=False if the enum does not have a default; you must either set a default or allow null/blank.","severity":"gotcha","affected_versions":">=4.0.0"}],"env_vars":null,"last_verified":"2026-05-01T00:00:00.000Z","next_check":"2026-07-30T00:00:00.000Z","problems":[{"fix":"Add null=True to the field definition, e.g., status = ChoicesField(choices_enum=Status, blank=True, null=True)","cause":"Since v4.0.0, blank=True requires null=True to avoid empty string ambiguity with non-string choices.","error":"django.core.exceptions.ImproperlyConfigured: ChoicesField 'status' cannot have blank=True without also having null=True"},{"fix":"Use Status.choices if you need the tuple of choices, or iterate over Status directly: for member in Status: ...","cause":"Trying to iterate over a single enum member instead of using .choices or .values.","error":"TypeError: 'Status' object is not iterable"},{"fix":"Assign enum members, e.g., obj.status = MyModel.Status.PUBLISHED, not obj.status = 'PUBLISHED'. For form submission, ensure the form field is also using choices_enum.","cause":"Passing a raw value that is not part of the enum, or missing the enum member conversion.","error":"django.core.exceptions.ValidationError: ['\"invalid\" is not a valid choice.']"}],"ecosystem":"pypi","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}