@@ -29,7 +29,7 @@ data where no ``Enum`` type coercion is possible.
2929
3030.. code-block :: python
3131
32- class MyModel (models .Model ):
32+ class StrictExample (models .Model ):
3333
3434 class EnumType (TextChoices ):
3535
@@ -44,13 +44,13 @@ data where no ``Enum`` type coercion is possible.
4444 max_length = 10
4545 )
4646
47- obj = MyModel ()
47+ obj = StrictExample ()
4848
4949 # set to a valid EnumType value
5050 obj.non_strict = ' 1'
5151 obj.full_clean()
5252 # when accessed from the db or after clean, will be an EnumType instance
53- assert obj.non_strict is MyModel .EnumType.ONE
53+ assert obj.non_strict is StrictExample .EnumType.ONE
5454
5555 # we can also store any string less than or equal to length 10
5656 obj.non_strict = ' arbitrary'
@@ -59,33 +59,32 @@ data where no ``Enum`` type coercion is possible.
5959 assert obj.non_strict == ' arbitrary'
6060
6161
62- ..
63- ``coerce``
64- ----------
65-
66- Setting this parameter to ``False `` will turn off the automatic conversion to
67- the field's ``Enum `` type while leaving all validation checks in place. It will
68- still be possible to set the field directly as an ``Enum `` instance:
62+ ``coerce ``
63+ ----------
6964
70- .. code-block :: python
65+ Setting this parameter to ``False `` will turn off the automatic conversion to
66+ the field's ``Enum `` type while leaving all validation checks in place. It will
67+ still be possible to set the field directly as an ``Enum `` instance:
7168
72- non_strict = EnumField(
73- EnumType,
74- strict = False ,
75- coerce = False ,
76- # it might be necessary to override max_length also, otherwise
77- # max_length will be 1
78- max_length = 10
79- )
69+ .. code-block :: python
8070
81- # set to a valid EnumType value
82- obj.non_strict = ' 1'
83- obj.full_clean()
71+ non_strict = EnumField(
72+ EnumType,
73+ strict = False ,
74+ coerce = False ,
75+ # it might be necessary to override max_length also, otherwise
76+ # max_length will be 1
77+ max_length = 10
78+ )
8479
85- # when accessed from the db or after clean, will be the primitive value
86- assert obj.non_strict = = ' 1'
87- assert isinstance ( obj.non_strict, str )
80+ # set to a valid EnumType value
81+ obj.non_strict = ' 1'
82+ obj.full_clean( )
8883
84+ # when accessed from the db or after clean, will be the primitive value
85+ assert obj.non_strict == ' 1'
86+ assert isinstance (obj.non_strict, str )
87+ assert not isinstance (obj.non_strict, StrictExample.EnumType)
8988
9089 enum-properties
9190###############
@@ -108,7 +107,7 @@ values that can be symmetrically mapped back to enumeration values:
108107 from django_enum import TextChoices # use instead of Django's TextChoices
109108 from django.db import models
110109
111- class MyModel (models.Model):
110+ class TextChoicesExample (models.Model):
112111
113112 class Color(TextChoices, s('rgb'), s('hex', case_fold=True)):
114113
@@ -123,8 +122,12 @@ values that can be symmetrically mapped back to enumeration values:
123122
124123 color = EnumField(Color)
125124
126- instance = MyModel.objects.create(color=MyModel.Color('FF0000'))
127- assert instance.color == MyModel.Color('Red') == MyModel.Color('R') == MyModel.Color((1, 0, 0))
125+ instance = TextChoicesExample.objects.create(
126+ color=TextChoicesExample.Color('FF0000')
127+ )
128+ assert instance.color == TextChoicesExample.Color('Red')
129+ assert instance.color == TextChoicesExample.Color('R')
130+ assert instance.color == TextChoicesExample.Color((1, 0, 0))
128131
129132 # direct comparison to any symmetric value also works
130133 assert instance.color == 'Red'
0 commit comments