Skip to content

Commit

Permalink
Merge pull request #78 from bckohan/v2.x.x
Browse files Browse the repository at this point in the history
V2.0.2
  • Loading branch information
bckohan authored Sep 25, 2024
2 parents d2b8fba + c22a949 commit 5a25b25
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 13 deletions.
4 changes: 2 additions & 2 deletions check.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
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, 1)
VERSION = (2, 0, 2)

__title__ = "Django Enum"
__version__ = ".".join(str(i) for i in VERSION)
Expand Down
20 changes: 11 additions & 9 deletions django_enum/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
**{ # type: ignore[call-overload]
condition: constraint,
"name": self.constraint_name(cls, name, self.enum),
"name": self.constraint_name(cls, self.name or name, self.enum),
}
),
]
Expand Down Expand Up @@ -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
),
}
),
]
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.2 (2024-09-25)
===================

* Fixed `Constraints fail when using a name argument <https://github.com/bckohan/django-enum/issues/77>`_

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

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.1"
version = "2.0.2"
description = "Full and natural support for enumerations as Django model fields."
authors = ["Brian Kohan <[email protected]>"]
license = "MIT"
Expand Down
11 changes: 11 additions & 0 deletions tests/djenum/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
21 changes: 21 additions & 0 deletions tests/test_name_override.py
Original file line number Diff line number Diff line change
@@ -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")

0 comments on commit 5a25b25

Please sign in to comment.