Skip to content

Commit 0e308e2

Browse files
authored
Merge pull request #75 from bckohan/v2.x.x
V2.x.x
2 parents 2639972 + 6a2c8bc commit 0e308e2

File tree

6 files changed

+36
-6
lines changed

6 files changed

+36
-6
lines changed

django_enum/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
__all__ = ["EnumField"]
1515

16-
VERSION = (2, 0, 0)
16+
VERSION = (2, 0, 1)
1717

1818
__title__ = "Django Enum"
1919
__version__ = ".".join(str(i) for i in VERSION)

django_enum/fields.py

+18-3
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,10 @@ class EnumField(
365365

366366
descriptor_class = ToPythonDeferredAttribute
367367

368+
default_error_messages: Any = { # mypy is stupid
369+
"invalid_choice": _("Value %(value)r is not a valid %(enum)r.")
370+
}
371+
368372
# use properties to disable setters
369373
@property
370374
def enum(self):
@@ -611,8 +615,12 @@ def to_python(self, value: Any) -> Union[Enum, Any]:
611615
if value is None:
612616
return value
613617
raise ValidationError(
614-
f"'{value}' is not a valid "
615-
f"{self.enum.__name__ if self.enum else ''}."
618+
self.error_messages["invalid_choice"],
619+
code="invalid_choice",
620+
params={
621+
"value": value,
622+
"enum": self.enum.__name__ if self.enum else "",
623+
},
616624
) from err
617625

618626
def get_default(self) -> Any:
@@ -648,7 +656,12 @@ def validate(self, value: Any, model_instance: Optional[Model]):
648656
self._try_coerce(value, force=True)
649657
except ValueError as err:
650658
raise ValidationError(
651-
str(err), code="invalid_choice", params={"value": value}
659+
self.error_messages["invalid_choice"],
660+
code="invalid_choice",
661+
params={
662+
"value": value,
663+
"enum": self.enum.__name__ if self.enum else "",
664+
},
652665
) from err
653666

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

785+
empty_values = [empty for empty in CharField.empty_values if empty != ""]
786+
772787
@property
773788
def primitive(self):
774789
return EnumField.primitive.fget(self) or str # type: ignore

doc/source/changelog.rst

+5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
Change Log
55
==========
66

7+
v2.0.1 (2024-09-16)
8+
===================
9+
10+
* Fixed `Unexpected ValueError instead of ValidationError <https://github.com./bckohan/django-enum/issues/74>`_
11+
712
v2.0.0 (2024-09-09)
813
===================
914

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "django-enum"
3-
version = "2.0.0"
3+
version = "2.0.1"
44
description = "Full and natural support for enumerations as Django model fields."
55
authors = ["Brian Kohan <[email protected]>"]
66
license = "MIT"

tests/djenum/models.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class EnumTester(models.Model):
5858

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

61-
text = EnumField(TextEnum, null=True, default=None, db_index=True, blank=False)
61+
text = EnumField(TextEnum, null=True, default=None, db_index=True, blank=True)
6262

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

tests/test_errors.py

+10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from django.test import TestCase
2+
from django.core.exceptions import ValidationError
23
from django_enum import EnumField
4+
from tests.djenum.models import EnumTester
35

46

57
class MiscOffNominalTests(TestCase):
@@ -11,6 +13,14 @@ def test_field_def_errors(self):
1113
class TestModel(Model):
1214
enum = EnumField()
1315

16+
def test_full_clean_raises_validation_error(self):
17+
with self.assertRaises(ValidationError):
18+
en = EnumTester(text="wrong")
19+
en.full_clean()
20+
21+
with self.assertRaises(ValidationError):
22+
EnumTester(text="").full_clean()
23+
1424
def test_variable_primitive_type(self):
1525
from enum import Enum
1626

0 commit comments

Comments
 (0)