You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+51-19Lines changed: 51 additions & 19 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -23,18 +23,19 @@
23
23
24
24
Full and natural support for [enumerations](https://docs.python.org/3/library/enum.html#enum.Enum) as Django model fields.
25
25
26
-
Many packages aim to ease usage of Python enumerations as model fields. Most were made obsolete when Django provided ``TextChoices`` and ``IntegerChoices`` types. The motivation for [django-enum](https://django-enum.readthedocs.io) was to:
26
+
Many packages aim to ease usage of Python enumerations as model fields. Most were superseded when Django provided ``TextChoices`` and ``IntegerChoices`` types. The motivation for [django-enum](https://django-enum.readthedocs.io) was to:
27
27
28
28
* Work with any Python PEP 435 Enum including those that do not derive from Django's TextChoices and IntegerChoices.
29
-
*Always automatically coerce fields to instances of the Enum type.
29
+
*Coerce fields to instances of the Enum type by default.
30
30
* Allow strict adherence to Enum values to be disabled.
31
31
* Handle migrations appropriately. (See [migrations](https://django-enum.readthedocs.io/en/latest/usage.html#migrations))
32
32
* Integrate as fully as possible with [Django's](https://www.djangoproject.com) existing level of enum support.
33
-
* Support [enum-properties](https://pypi.org/project/enum-properties)and dataclass enumerations to enable richer enumeration types.
33
+
* Support [enum-properties](https://pypi.org/project/enum-properties) to enable richer enumeration types. (A less awkward alternative to dataclass enumerations with more features)
34
34
* Represent enum fields with the smallest possible column type.
35
-
*Provide bit mask functionality using standard Python Flag enumerations.
35
+
*Support bit mask queries using standard Python Flag enumerations.
36
36
* Be as simple and light-weight an extension to core [Django](https://www.djangoproject.com) as possible.
37
-
* Optionally enforce enumeration value consistency at the database level using check constraints.
37
+
* Enforce enumeration value consistency at the database level using check constraints by default.
38
+
* (TODO) Support native database enumeration column types when available.
38
39
39
40
[django-enum](https://django-enum.readthedocs.io) works in concert with [Django's](https://www.djangoproject.com) built in ``TextChoices`` and ``IntegerChoices`` to provide a new model field type, ``EnumField``, that resolves the correct native [Django](https://www.djangoproject.com) field type for the given enumeration based on its value type and range. For example, ``IntegerChoices`` that contain values between 0 and 32767 become [PositiveSmallIntegerField](https://docs.djangoproject.com/en/stable/ref/models/fields/#positivesmallintegerfield).
40
41
@@ -62,8 +63,8 @@ Many packages aim to ease usage of Python enumerations as model fields. Most wer
``EnumField``**is more than just an alias. The fields are now assignable and accessible as their enumeration type rather than by-value:**
@@ -82,24 +83,29 @@ Many packages aim to ease usage of Python enumerations as model fields. Most wer
82
83
assert instance.int_enum.value ==3
83
84
```
84
85
85
-
[django-enum](https://django-enum.readthedocs.io) also provides ``IntegerChoices`` and ``TextChoices`` types that extend from [enum-properties](https://pypi.org/project/enum-properties) which makes possible very rich enumeration fields.
86
+
## Complex Enumerations
87
+
88
+
[django-enum](https://django-enum.readthedocs.io) supports enum types that do not derive from Django's ``IntegerChoices`` and ``TextChoices``. This allows us to use other libs like [enum-properties](https://pypi.org/project/enum-properties) which makes possible very rich enumeration fields:
86
89
87
90
``?> pip install enum-properties``
88
91
89
92
```python
90
93
91
-
from enum_properties import s
92
-
from django_enum import TextChoices # use instead of Django's TextChoices
Consider using [django-render-static](https://pypi.org/project/django-render-static) to make your enumerations [DRY](https://en.wikipedia.org/wiki/Don%27t_repeat_yourself) across the full stack!
152
+
## Flag Support
147
153
148
-
Please report bugs and discuss features on the [issues page](https://github.com/bckohan/django-enum/issues).
154
+
``FlagEnum`` types are also seamlessly supported! This allows a database column to behave like a bit mask and is an alternative to multiple boolean columns. There are (mostly positive) performance implications for using a bit mask instead of booleans depending on the size of the bit mask and the types of queries you will run against it. For bit masks more than a few bits long the size reduction both speeds up queries and reduces the required storage space. See the documentation for [discussion and benchmarks]().
149
155
150
-
[Contributions](https://github.com/bckohan/django-enum/blob/main/CONTRIBUTING.md) are encouraged!
156
+
```python
157
+
158
+
classPermissions(IntFlag):
159
+
160
+
READ=0**2
161
+
WRITE=1**2
162
+
EXECUTE=2**3
163
+
164
+
165
+
classFlagExample(models.Model):
166
+
167
+
permissions = EnumField(Permissions)
151
168
152
-
[Full documentation at read the docs.](https://django-enum.readthedocs.io)
@@ -176,3 +198,13 @@ Like with Django, Postgres is the preferred database for support. The full test
176
198
**See the [latest test runs](https://github.com/bckohan/django-enum/actions/workflows/test.yml) for our current test matrix**
177
199
178
200
*For Oracle, only the latest version of the free database is tested against the minimum and maximum supported versions of Python, Django and the cx-Oracle driver.*
201
+
202
+
## Further Reading
203
+
204
+
Consider using [django-render-static](https://pypi.org/project/django-render-static) to make your enumerations [DRY](https://en.wikipedia.org/wiki/Don%27t_repeat_yourself) across the full stack!
205
+
206
+
Please report bugs and discuss features on the [issues page](https://github.com/bckohan/django-enum/issues).
207
+
208
+
[Contributions](https://github.com/bckohan/django-enum/blob/main/CONTRIBUTING.md) are encouraged!
209
+
210
+
[Full documentation at read the docs.](https://django-enum.readthedocs.io)
0 commit comments