Skip to content

V2.x.x #75

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 2 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion django_enum/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

__all__ = ["EnumField"]

VERSION = (2, 0, 0)
VERSION = (2, 0, 1)

__title__ = "Django Enum"
__version__ = ".".join(str(i) for i in VERSION)
Expand Down
21 changes: 18 additions & 3 deletions django_enum/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,10 @@ class EnumField(

descriptor_class = ToPythonDeferredAttribute

default_error_messages: Any = { # mypy is stupid
"invalid_choice": _("Value %(value)r is not a valid %(enum)r.")
}

# use properties to disable setters
@property
def enum(self):
Expand Down Expand Up @@ -611,8 +615,12 @@ def to_python(self, value: Any) -> Union[Enum, Any]:
if value is None:
return value
raise ValidationError(
f"'{value}' is not a valid "
f"{self.enum.__name__ if self.enum else ''}."
self.error_messages["invalid_choice"],
code="invalid_choice",
params={
"value": value,
"enum": self.enum.__name__ if self.enum else "",
},
) from err

def get_default(self) -> Any:
Expand Down Expand Up @@ -648,7 +656,12 @@ def validate(self, value: Any, model_instance: Optional[Model]):
self._try_coerce(value, force=True)
except ValueError as err:
raise ValidationError(
str(err), code="invalid_choice", params={"value": value}
self.error_messages["invalid_choice"],
code="invalid_choice",
params={
"value": value,
"enum": self.enum.__name__ if self.enum else "",
},
) from err

def formfield(self, form_class=None, choices_form_class=None, **kwargs):
Expand Down Expand Up @@ -769,6 +782,8 @@ class EnumCharField(EnumField[Type[str]], CharField):
A database field supporting enumerations with character values.
"""

empty_values = [empty for empty in CharField.empty_values if empty != ""]

@property
def primitive(self):
return EnumField.primitive.fget(self) or str # type: ignore
Expand Down
5 changes: 5 additions & 0 deletions doc/source/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
Change Log
==========

v2.0.1 (2024-09-16)
===================

* Fixed `Unexpected ValueError instead of ValidationError <https://github.com/bckohan/django-enum/issues/74>`_

v2.0.0 (2024-09-09)
===================

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "django-enum"
version = "2.0.0"
version = "2.0.1"
description = "Full and natural support for enumerations as Django model fields."
authors = ["Brian Kohan <bckohan@gmail.com>"]
license = "MIT"
Expand Down
2 changes: 1 addition & 1 deletion tests/djenum/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class EnumTester(models.Model):

constant = EnumField(Constants, null=True, default=None, db_index=True, blank=True)

text = EnumField(TextEnum, null=True, default=None, db_index=True, blank=False)
text = EnumField(TextEnum, null=True, default=None, db_index=True, blank=True)

extern = EnumField(ExternEnum, null=True, default=None, db_index=True, blank=True)

Expand Down
10 changes: 10 additions & 0 deletions tests/test_errors.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from django.test import TestCase
from django.core.exceptions import ValidationError
from django_enum import EnumField
from tests.djenum.models import EnumTester


class MiscOffNominalTests(TestCase):
Expand All @@ -11,6 +13,14 @@ def test_field_def_errors(self):
class TestModel(Model):
enum = EnumField()

def test_full_clean_raises_validation_error(self):
with self.assertRaises(ValidationError):
en = EnumTester(text="wrong")
en.full_clean()

with self.assertRaises(ValidationError):
EnumTester(text="").full_clean()

def test_variable_primitive_type(self):
from enum import Enum

Expand Down