File: models.py

package info (click to toggle)
django-choices-field 3.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 192 kB
  • sloc: python: 463; sh: 18; makefile: 3
file content (82 lines) | stat: -rw-r--r-- 2,526 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import enum
import sys

from django.db import models
from django.utils.translation import gettext_lazy as _

from django_choices_field import IntegerChoicesField, TextChoicesField
from django_choices_field.fields import IntegerChoicesFlagField
from django_choices_field.types import IntegerChoicesFlag


class MyModel(models.Model):
    class TextEnum(models.TextChoices):
        C_FOO = "foo", "T Foo Description"
        C_BAR = "bar", "T Bar Description"

    class IntegerEnum(models.IntegerChoices):
        I_FOO = 1, "I Foo Description"
        I_BAR = 2, "I Bar Description"

    class IntegerFlagEnum(IntegerChoicesFlag):
        IF_FOO = (
            enum.auto() if sys.version_info >= (3, 11) else 1,
            "IF Foo Description",  # type: ignore
        )
        IF_BAR = (
            enum.auto() if sys.version_info >= (3, 11) else 2,
            "IF Bar Description",  # type: ignore
        )
        IF_BIN = (
            enum.auto() if sys.version_info >= (3, 11) else 4,
            "IF Bin Description",  # type: ignore
        )

    class IntegerFlagEnumTranslated(IntegerChoicesFlag):
        IF_FOO = (
            enum.auto() if sys.version_info >= (3, 11) else 1,
            _("IF Foo Description"),  # type: ignore
        )
        IF_BAR = (
            enum.auto() if sys.version_info >= (3, 11) else 2,
            _("IF Bar Description"),  # type: ignore
        )
        IF_BIN = (
            enum.auto() if sys.version_info >= (3, 11) else 4,
            _("IF Bin Description"),  # type: ignore
        )

    objects = models.Manager["MyModel"]()  # type: ignore

    c_field = TextChoicesField(
        choices_enum=TextEnum,
        default=TextEnum.C_FOO,
    )
    c_field_nullable = TextChoicesField(
        choices_enum=TextEnum,
        null=True,
    )
    i_field = IntegerChoicesField(
        choices_enum=IntegerEnum,
        default=IntegerEnum.I_FOO,
    )
    i_field_nullable = IntegerChoicesField(
        choices_enum=IntegerEnum,
        null=True,
    )
    if_field = IntegerChoicesFlagField(
        choices_enum=IntegerFlagEnum,
        default=IntegerFlagEnum.IF_FOO,
    )
    if_field_nullable = IntegerChoicesFlagField(
        choices_enum=IntegerFlagEnum,
        null=True,
    )
    ift_field = IntegerChoicesFlagField(
        choices_enum=IntegerFlagEnumTranslated,
        default=IntegerFlagEnumTranslated.IF_FOO,
    )
    ift_field_nullable = IntegerChoicesFlagField(
        choices_enum=IntegerFlagEnumTranslated,
        null=True,
    )