Skip to content

Commit 41ed7cb

Browse files
committed
doc updates
1 parent cb13d69 commit 41ed7cb

File tree

1 file changed

+36
-10
lines changed

1 file changed

+36
-10
lines changed

doc/source/index.rst

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,23 @@ Django Enum
66

77
Full and natural support for enumerations_ as Django model fields.
88

9-
`django-enum <https://django-enum.readthedocs.io/en/latest/>`_ works in concert
10-
with Django_'s built in ``TextChoices`` and ``IntegerChoices`` to provide a new
11-
model field type, ``EnumField``, that resolves the correct native Django_ field
12-
type for the given enumeration based on its value type and range. For example,
13-
``IntegerChoices`` that contain values between 0 and 32767 become
14-
`PositiveSmallIntegerField <https://docs.djangoproject.com/en/stable/ref/models/fields/#positivesmallintegerfield>`_.
9+
Many packages aim to ease usage of Python enumerations as model fields. Most
10+
were made obsolete when Django provided ``TextChoices`` and ``IntegerChoices``
11+
types. The motivation for django-enum was to:
12+
13+
* Always automatically coerce fields to instances of the Enum type.
14+
* Allow strict adherence to Enum values to be disabled.
15+
* Handle migrations appropriately. (See `migrations <https://django-enum.readthedocs.io/en/latest/usage.html#migrations>`_)
16+
* Integrate as fully as possible with Django_'s existing level of enum support.
17+
* Integrate with enum-properties_ to enable richer enumeration types.
18+
* Represent enum fields with the smallest possible column type.
19+
* Be as simple and light-weight an extension to core Django as possible.
20+
21+
django-enum works in concert with Django_'s built in ``TextChoices`` and
22+
``IntegerChoices`` to provide a new model field type, ``EnumField``, that
23+
resolves the correct native Django_ field type for the given enumeration based
24+
on its value type and range. For example, ``IntegerChoices`` that contain
25+
values between 0 and 32767 become `PositiveSmallIntegerField <https://docs.djangoproject.com/en/stable/ref/models/fields/#positivesmallintegerfield>`_.
1526

1627
.. code:: python
1728
@@ -68,7 +79,7 @@ enum-properties_ which makes possible very rich enumeration fields.
6879
from django_enum import TextChoices # use instead of Django's TextChoices
6980
from django.db import models
7081
71-
class MyModel(models.Model):
82+
class TextChoicesExample(models.Model):
7283
7384
class Color(TextChoices, s('rgb'), s('hex', case_fold=True)):
7485
@@ -83,15 +94,30 @@ enum-properties_ which makes possible very rich enumeration fields.
8394
8495
color = EnumField(Color)
8596
86-
instance = MyModel.objects.create(color=MyModel.Color('FF0000'))
87-
97+
instance = TextChoicesExample.objects.create(
98+
color=TextChoicesExample.Color('FF0000')
99+
)
88100
assert instance.color == TextChoicesExample.Color('Red')
89101
assert instance.color == TextChoicesExample.Color('R')
90102
assert instance.color == TextChoicesExample.Color((1, 0, 0))
91103
92-
# save by any symmetric value or enum type instance
104+
# direct comparison to any symmetric value also works
105+
assert instance.color == 'Red'
106+
assert instance.color == 'R'
107+
assert instance.color == (1, 0, 0)
108+
109+
# save by any symmetric value
93110
instance.color = 'FF0000'
111+
112+
# access any enum property right from the model field
94113
assert instance.color.hex == 'ff0000'
114+
115+
# this also works!
116+
assert instance.color == 'ff0000'
117+
118+
# and so does this!
119+
assert instance.color == 'FF0000'
120+
95121
instance.save()
96122
97123
# filtering works by any symmetric value or enum type instance

0 commit comments

Comments
 (0)