Skip to content

Commit 56ef725

Browse files
committed
readme updates
1 parent 05276f7 commit 56ef725

File tree

1 file changed

+33
-15
lines changed

1 file changed

+33
-15
lines changed

README.md

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,30 @@ Many packages aim to ease usage of Python enumerations as model fields. Most wer
8383
assert instance.int_enum.value == 3
8484
```
8585

86+
## Flag Support
87+
88+
``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]().
89+
90+
```python
91+
92+
class Permissions(IntFlag):
93+
94+
READ = 0**2
95+
WRITE = 1**2
96+
EXECUTE = 2**3
97+
98+
99+
class FlagExample(models.Model):
100+
101+
permissions = EnumField(Permissions)
102+
103+
104+
FlagExample.objects.create(permissions=Permissions.READ | Permissions.WRITE)
105+
106+
# get all models with RW:
107+
FlagExample.objects.filter(permissions__all=Permissions.READ | Permissions.WRITE)
108+
```
109+
86110
## Complex Enumerations
87111

88112
[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:
@@ -149,28 +173,22 @@ Many packages aim to ease usage of Python enumerations as model fields. Most wer
149173
assert TextChoicesExample.objects.filter(color='FF0000').first() == instance
150174
```
151175

152-
## Flag Support
153-
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]().
176+
While they should be unnecessary if you need to integrate with code that expects an interface fully compatible with Django's ``TextChoices`` and ``IntegerChoices`` django-enum provides ``TextChoices`` and ``IntegerChoices`` types that derive from enum-properties and Django's enum types. So the above enumeration could also be written:
155177

156178
```python
157179

158-
class Permissions(IntFlag):
159-
160-
READ = 0**2
161-
WRITE = 1**2
162-
EXECUTE = 2**3
163-
180+
from django_enum.choices import TextChoices
164181

165-
class FlagExample(models.Model):
182+
class Color(TextChoices):
166183

167-
permissions = EnumField(Permissions)
184+
rgb: Annotated[t.Tuple[int, int, int], Symmetric()]
185+
hex: Annotated[str, Symmetric(case_fold=True)]
168186

187+
# name value label rgb hex
188+
RED = "R", "Red", (1, 0, 0), "ff0000"
189+
GREEN = "G", "Green", (0, 1, 0), "00ff00"
190+
BLUE = "B", "Blue", (0, 0, 1), "0000ff"
169191

170-
FlagExample.objects.create(permissions=Permissions.READ | Permissions.WRITE)
171-
172-
# get all models with RW:
173-
FlagExample.objects.filter(permissions__all=Permissions.READ | Permissions.WRITE)
174192
```
175193

176194
## Installation

0 commit comments

Comments
 (0)