Skip to content

Commit ca73c88

Browse files
committed
docs for intial release finalized, few more example tests
1 parent 3bfd4c3 commit ca73c88

File tree

7 files changed

+292
-10
lines changed

7 files changed

+292
-10
lines changed

README.rst

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,27 @@ possible very rich enumeration fields.
130130
# save by any symmetric value
131131
instance.color = 'FF0000'
132132
instance.full_clean()
133+
134+
# access any enum property right from the model field
133135
assert instance.color.hex == 'ff0000'
136+
137+
# this also works!
138+
assert instance.color == 'ff0000'
139+
140+
# and so does this!
141+
assert instance.color == 'FF0000'
142+
134143
instance.save()
135144
145+
# filtering works by any symmetric value or enum type instance
146+
assert TextChoicesExample.objects.filter(
147+
color=TextChoicesExample.Color.RED
148+
).first() == instance
149+
150+
assert TextChoicesExample.objects.filter(color=(1, 0, 0)).first() == instance
151+
152+
assert TextChoicesExample.objects.filter(color='FF0000').first() == instance
153+
136154
137155
.. note::
138156

django_enum/__init__.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
IntegerChoices,
1515
TextChoices,
1616
)
17+
from django_enum.filters import (
18+
FilterSet,
19+
EnumFilter
20+
)
1721
from django_enum.fields import (
1822
EnumBigIntegerField,
1923
EnumCharField,
@@ -41,7 +45,9 @@
4145
'IntegerChoices',
4246
'FloatChoices',
4347
'DjangoEnumPropertiesMeta',
44-
'EnumChoiceField'
48+
'EnumChoiceField',
49+
'FilterSet',
50+
'EnumFilter'
4551
]
4652

4753
VERSION = (1, 0, 0)

django_enum/filters.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@ class Color(TextChoices, s('rgb'), s('hex', case_fold=True)):
3535

3636
def __init__(self, *, enum, **kwargs):
3737
self.enum = enum
38-
super().__init__(enum=enum, choices=self.enum.choices, **kwargs)
38+
super().__init__(
39+
enum=enum,
40+
choices=kwargs.pop('choices', self.enum.choices),
41+
**kwargs
42+
)
3943

4044

4145
class FilterSet(filterset.FilterSet):

django_enum/tests/tests.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2977,9 +2977,51 @@ def test_color(self):
29772977
# save by any symmetric value
29782978
instance.color = 'FF0000'
29792979
instance.full_clean()
2980+
2981+
# access any property right from the model field
29802982
self.assertTrue(instance.color.hex == 'ff0000')
2983+
2984+
# this also works!
2985+
self.assertTrue(instance.color == 'ff0000')
2986+
2987+
# and so does this!
2988+
self.assertTrue(instance.color == 'FF0000')
2989+
29812990
instance.save()
29822991

2992+
self.assertTrue(
2993+
TextChoicesExample.objects.filter(
2994+
color=TextChoicesExample.Color.RED
2995+
).first() == instance
2996+
)
2997+
2998+
self.assertTrue(
2999+
TextChoicesExample.objects.filter(
3000+
color=(1, 0, 0)
3001+
).first() == instance
3002+
)
3003+
3004+
self.assertTrue(
3005+
TextChoicesExample.objects.filter(
3006+
color='FF0000'
3007+
).first() == instance
3008+
)
3009+
3010+
from django_enum import EnumChoiceField
3011+
from django.forms import ModelForm
3012+
3013+
class TextChoicesExampleForm(ModelForm):
3014+
color = EnumChoiceField(TextChoicesExample.Color)
3015+
3016+
class Meta:
3017+
model = TextChoicesExample
3018+
fields = '__all__'
3019+
3020+
# this is possible
3021+
form = TextChoicesExampleForm({'color': 'FF0000'})
3022+
form.save()
3023+
self.assertTrue(form.instance.color == TextChoicesExample.Color.RED)
3024+
29833025
def test_strict(self):
29843026
from django_enum.tests.examples.models import StrictExample
29853027
obj = StrictExample()
@@ -3010,6 +3052,16 @@ def test_basic(self):
30103052
self.assertTrue(instance.int_enum == MyModel.IntEnum['THREE'])
30113053
self.assertTrue(instance.int_enum.value == 3)
30123054

3055+
self.assertRaises(
3056+
ValueError,
3057+
MyModel.objects.create,
3058+
txt_enum='AA',
3059+
int_enum=3
3060+
)
3061+
3062+
instance.txt_enum='AA'
3063+
self.assertRaises(ValidationError, instance.full_clean)
3064+
30133065
def test_no_coerce(self):
30143066
from django_enum.tests.examples.models import NoCoerceExample
30153067

doc/source/index.rst

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,25 @@ enum-properties_ which makes possible very rich enumeration fields.
8484
color = EnumField(Color)
8585
8686
instance = MyModel.objects.create(color=MyModel.Color('FF0000'))
87-
assert instance.color == MyModel.Color('Red') == MyModel.Color('R') == MyModel.Color((1, 0, 0))
8887
89-
# save back by any symmetric value
88+
assert instance.color == TextChoicesExample.Color('Red')
89+
assert instance.color == TextChoicesExample.Color('R')
90+
assert instance.color == TextChoicesExample.Color((1, 0, 0))
91+
92+
# save by any symmetric value or enum type instance
9093
instance.color = 'FF0000'
9194
instance.full_clean()
9295
assert instance.color.hex == 'ff0000'
9396
instance.save()
9497
98+
# filtering works by any symmetric value or enum type instance
99+
assert TextChoicesExample.objects.filter(
100+
color=TextChoicesExample.Color.RED
101+
).first() == instance
102+
103+
assert TextChoicesExample.objects.filter(color=(1, 0, 0)).first() == instance
104+
105+
assert TextChoicesExample.objects.filter(color='FF0000').first() == instance
95106
96107
.. note::
97108

@@ -125,8 +136,7 @@ Installation
125136
``django-enum`` has several optional dependencies that are not pulled in
126137
by default. To utilize the
127138
enum-properties_ choice types you must `pip install enum-properties` and
128-
to use the ``EnumFilter`` type for
129-
`django-filter <https://pypi.org/project/django-filter/>`_ you must
139+
to use the ``EnumFilter`` type for django-filter_ you must
130140
`pip install django-filter`.
131141

132142
If features are utilized that require a missing optional dependency an

doc/source/refs.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@
1313
.. _full_clean: https://docs.djangoproject.com/en/stable/ref/models/instances/#django.db.models.Model.full_clean
1414
.. _DRY: https://en.wikipedia.org/wiki/Don%27t_repeat_yourself
1515
.. _enum-properties: https://pypi.org/project/enum-properties
16+
.. _django-filter: https://pypi.org/project/django-filter

0 commit comments

Comments
 (0)