From 88ccb1d9be4526983cb0c39fa408b2d6868371e6 Mon Sep 17 00:00:00 2001 From: Brian Kohan Date: Mon, 16 Sep 2024 16:12:09 -0700 Subject: [PATCH 1/2] remove empty string from empty_fields #74 --- django_enum/fields.py | 21 ++++++++++++++++++--- tests/djenum/models.py | 2 +- tests/test_errors.py | 10 ++++++++++ 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/django_enum/fields.py b/django_enum/fields.py index e0f45fb..9835446 100644 --- a/django_enum/fields.py +++ b/django_enum/fields.py @@ -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): @@ -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: @@ -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): @@ -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 diff --git a/tests/djenum/models.py b/tests/djenum/models.py index b1905e0..c40d48e 100644 --- a/tests/djenum/models.py +++ b/tests/djenum/models.py @@ -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) diff --git a/tests/test_errors.py b/tests/test_errors.py index 6480d01..6247f4a 100644 --- a/tests/test_errors.py +++ b/tests/test_errors.py @@ -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): @@ -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 From 6a2c8bcf87851b4eb1668459138f4d17222d86ed Mon Sep 17 00:00:00 2001 From: Brian Kohan Date: Mon, 16 Sep 2024 16:56:13 -0700 Subject: [PATCH 2/2] incr version, update changelog --- django_enum/__init__.py | 2 +- doc/source/changelog.rst | 5 +++++ pyproject.toml | 2 +- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/django_enum/__init__.py b/django_enum/__init__.py index 99f2701..1e47659 100644 --- a/django_enum/__init__.py +++ b/django_enum/__init__.py @@ -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) diff --git a/doc/source/changelog.rst b/doc/source/changelog.rst index aa799e4..3fa5ce9 100644 --- a/doc/source/changelog.rst +++ b/doc/source/changelog.rst @@ -4,6 +4,11 @@ Change Log ========== +v2.0.1 (2024-09-16) +=================== + +* Fixed `Unexpected ValueError instead of ValidationError `_ + v2.0.0 (2024-09-09) =================== diff --git a/pyproject.toml b/pyproject.toml index 3a80ab3..16cd8d5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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 "] license = "MIT"