From 012bcb6c3b84ac3da9b0231541cbacb2664cb796 Mon Sep 17 00:00:00 2001 From: Brian Kohan Date: Wed, 25 Sep 2024 07:46:21 -0700 Subject: [PATCH 1/2] fix #77 --- check.sh | 4 ++-- django_enum/__init__.py | 2 +- django_enum/fields.py | 18 ++++++++++-------- doc/source/changelog.rst | 5 +++++ pyproject.toml | 2 +- tests/djenum/models.py | 11 +++++++++++ tests/test_name_override.py | 21 +++++++++++++++++++++ 7 files changed, 51 insertions(+), 12 deletions(-) create mode 100644 tests/test_name_override.py diff --git a/check.sh b/check.sh index ee6390d..e2e5a43 100755 --- a/check.sh +++ b/check.sh @@ -13,10 +13,10 @@ else fi poetry run mypy django_enum -poetry check -poetry run pip check cd ./doc poetry run doc8 --ignore-path build --max-line-length 100 -q +poetry check +poetry run pip check # check for broken links in the docs ############ set +e diff --git a/django_enum/__init__.py b/django_enum/__init__.py index 1e47659..8c25794 100644 --- a/django_enum/__init__.py +++ b/django_enum/__init__.py @@ -13,7 +13,7 @@ __all__ = ["EnumField"] -VERSION = (2, 0, 1) +VERSION = (2, 0, 2) __title__ = "Django Enum" __version__ = ".".join(str(i) for i in VERSION) diff --git a/django_enum/fields.py b/django_enum/fields.py index 9835446..132d4b5 100644 --- a/django_enum/fields.py +++ b/django_enum/fields.py @@ -753,19 +753,19 @@ def contribute_to_class( elif self.constrained and self.enum: constraint = Q( **{ - f"{name}__in": [ + f"{self.name or name}__in": [ self._coerce_to_value_type(value) for value in values(self.enum) ] } ) if self.null: - constraint |= Q(**{f"{name}__isnull": True}) + constraint |= Q(**{f"{self.name or name}__isnull": True}) cls._meta.constraints = [ *cls._meta.constraints, CheckConstraint( **{ # type: ignore[arg-type] condition: constraint, - "name": self.constraint_name(cls, name, self.enum), + "name": self.constraint_name(cls, self.name or name, self.enum), } ), ] @@ -1185,19 +1185,21 @@ def contribute_to_class( if is_strict or is_conform or (is_eject and self.strict) and flags: constraint = ( - Q(**{f"{name}__gte": min(*flags)}) - & Q(**{f"{name}__lte": reduce(or_, flags)}) - ) | Q(**{name: 0}) + Q(**{f"{self.name or name}__gte": min(*flags)}) + & Q(**{f"{self.name or name}__lte": reduce(or_, flags)}) + ) | Q(**{self.name or name: 0}) if self.null: - constraint |= Q(**{f"{name}__isnull": True}) + constraint |= Q(**{f"{self.name or name}__isnull": True}) cls._meta.constraints = [ *cls._meta.constraints, CheckConstraint( **{ condition: constraint, - "name": self.constraint_name(cls, name, self.enum), + "name": self.constraint_name( + cls, self.name or name, self.enum + ), } ), ] diff --git a/doc/source/changelog.rst b/doc/source/changelog.rst index e989f84..a0cd2dc 100644 --- a/doc/source/changelog.rst +++ b/doc/source/changelog.rst @@ -4,6 +4,11 @@ Change Log ========== +v2.0.2 (2024-09-25) +=================== + +* Fixed `Constraints fail when using a name argument `_ + v2.0.1 (2024-09-16) =================== diff --git a/pyproject.toml b/pyproject.toml index 16cd8d5..3e93baa 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "django-enum" -version = "2.0.1" +version = "2.0.2" description = "Full and natural support for enumerations as Django model fields." authors = ["Brian Kohan "] license = "MIT" diff --git a/tests/djenum/models.py b/tests/djenum/models.py index c40d48e..f26bdd8 100644 --- a/tests/djenum/models.py +++ b/tests/djenum/models.py @@ -317,3 +317,14 @@ class CustomPrimitiveTestModel(models.Model): class TestNullableFloat(models.Model): nullable_float = EnumField(NullableConstants, default=None, blank=True, null=True) + + +class NameOverrideTest(models.Model): + class TextEnum(models.TextChoices): + VALUE0 = "V0", "Value 0" + VALUE1 = "V1", "Value 1" + VALUE2 = "V2", "Value 2" + + txt_enum = EnumField( + TextEnum, name="enum_field", null=True, blank=True, default=None + ) diff --git a/tests/test_name_override.py b/tests/test_name_override.py new file mode 100644 index 0000000..5b05b05 --- /dev/null +++ b/tests/test_name_override.py @@ -0,0 +1,21 @@ +from django.test import TestCase +from pathlib import Path +from decimal import Decimal +from django_enum.forms import EnumChoiceField +from django_enum.utils import choices + + +class TestNameOverride(TestCase): + """ + https://github.com/bckohan/django-enum/issues/77 + """ + + def test_name_override(self): + from tests.djenum.models import NameOverrideTest + + self.assertEqual(NameOverrideTest._meta.get_field("enum_field").primitive, str) + + NameOverrideTest.objects.create(enum_field="V1") + obj = NameOverrideTest.objects.first() + self.assertEqual(obj.enum_field, "V1") + self.assertEqual(obj.get_enum_field_display(), "Value 1") From c22a94915d8a139c056461d47289df75076e6351 Mon Sep 17 00:00:00 2001 From: Brian Kohan Date: Wed, 25 Sep 2024 07:53:56 -0700 Subject: [PATCH 2/2] type checker ignore --- django_enum/fields.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/django_enum/fields.py b/django_enum/fields.py index 132d4b5..409a1b4 100644 --- a/django_enum/fields.py +++ b/django_enum/fields.py @@ -763,7 +763,7 @@ def contribute_to_class( cls._meta.constraints = [ *cls._meta.constraints, CheckConstraint( - **{ # type: ignore[arg-type] + **{ # type: ignore[call-overload] condition: constraint, "name": self.constraint_name(cls, self.name or name, self.enum), }