Skip to content

Commit 5a25b25

Browse files
authored
Merge pull request #78 from bckohan/v2.x.x
V2.0.2
2 parents d2b8fba + c22a949 commit 5a25b25

File tree

7 files changed

+52
-13
lines changed

7 files changed

+52
-13
lines changed

check.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ else
1313
fi
1414

1515
poetry run mypy django_enum
16-
poetry check
17-
poetry run pip check
1816
cd ./doc
1917
poetry run doc8 --ignore-path build --max-line-length 100 -q
18+
poetry check
19+
poetry run pip check
2020
# check for broken links in the docs ############
2121
set +e
2222

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, 1)
16+
VERSION = (2, 0, 2)
1717

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

django_enum/fields.py

+11-9
Original file line numberDiff line numberDiff line change
@@ -753,19 +753,19 @@ def contribute_to_class(
753753
elif self.constrained and self.enum:
754754
constraint = Q(
755755
**{
756-
f"{name}__in": [
756+
f"{self.name or name}__in": [
757757
self._coerce_to_value_type(value) for value in values(self.enum)
758758
]
759759
}
760760
)
761761
if self.null:
762-
constraint |= Q(**{f"{name}__isnull": True})
762+
constraint |= Q(**{f"{self.name or name}__isnull": True})
763763
cls._meta.constraints = [
764764
*cls._meta.constraints,
765765
CheckConstraint(
766-
**{ # type: ignore[arg-type]
766+
**{ # type: ignore[call-overload]
767767
condition: constraint,
768-
"name": self.constraint_name(cls, name, self.enum),
768+
"name": self.constraint_name(cls, self.name or name, self.enum),
769769
}
770770
),
771771
]
@@ -1185,19 +1185,21 @@ def contribute_to_class(
11851185

11861186
if is_strict or is_conform or (is_eject and self.strict) and flags:
11871187
constraint = (
1188-
Q(**{f"{name}__gte": min(*flags)})
1189-
& Q(**{f"{name}__lte": reduce(or_, flags)})
1190-
) | Q(**{name: 0})
1188+
Q(**{f"{self.name or name}__gte": min(*flags)})
1189+
& Q(**{f"{self.name or name}__lte": reduce(or_, flags)})
1190+
) | Q(**{self.name or name: 0})
11911191

11921192
if self.null:
1193-
constraint |= Q(**{f"{name}__isnull": True})
1193+
constraint |= Q(**{f"{self.name or name}__isnull": True})
11941194

11951195
cls._meta.constraints = [
11961196
*cls._meta.constraints,
11971197
CheckConstraint(
11981198
**{
11991199
condition: constraint,
1200-
"name": self.constraint_name(cls, name, self.enum),
1200+
"name": self.constraint_name(
1201+
cls, self.name or name, self.enum
1202+
),
12011203
}
12021204
),
12031205
]

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.2 (2024-09-25)
8+
===================
9+
10+
* Fixed `Constraints fail when using a name argument <https://github.com/bckohan/django-enum/issues/77>`_
11+
712
v2.0.1 (2024-09-16)
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.1"
3+
version = "2.0.2"
44
description = "Full and natural support for enumerations as Django model fields."
55
authors = ["Brian Kohan <bckohan@gmail.com>"]
66
license = "MIT"

tests/djenum/models.py

+11
Original file line numberDiff line numberDiff line change
@@ -317,3 +317,14 @@ class CustomPrimitiveTestModel(models.Model):
317317

318318
class TestNullableFloat(models.Model):
319319
nullable_float = EnumField(NullableConstants, default=None, blank=True, null=True)
320+
321+
322+
class NameOverrideTest(models.Model):
323+
class TextEnum(models.TextChoices):
324+
VALUE0 = "V0", "Value 0"
325+
VALUE1 = "V1", "Value 1"
326+
VALUE2 = "V2", "Value 2"
327+
328+
txt_enum = EnumField(
329+
TextEnum, name="enum_field", null=True, blank=True, default=None
330+
)

tests/test_name_override.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from django.test import TestCase
2+
from pathlib import Path
3+
from decimal import Decimal
4+
from django_enum.forms import EnumChoiceField
5+
from django_enum.utils import choices
6+
7+
8+
class TestNameOverride(TestCase):
9+
"""
10+
https://github.com/bckohan/django-enum/issues/77
11+
"""
12+
13+
def test_name_override(self):
14+
from tests.djenum.models import NameOverrideTest
15+
16+
self.assertEqual(NameOverrideTest._meta.get_field("enum_field").primitive, str)
17+
18+
NameOverrideTest.objects.create(enum_field="V1")
19+
obj = NameOverrideTest.objects.first()
20+
self.assertEqual(obj.enum_field, "V1")
21+
self.assertEqual(obj.get_enum_field_display(), "Value 1")

0 commit comments

Comments
 (0)