Django3.0IntegerChoicesDjango2.x使
Django 3.0 django.db.models Choices, TextChoices, IntegerChoices

from django.db import models
class Task(models.Model):
State = models.IntegerChoices('State', ' ')
state = models.IntegerField('', choices=State.choices)
"""
>>> Task.State.choices
((1, ''), (2, ''), (3, ''), (4, ''))
"""

便使
使Django 2.x 使
django/db/models/enums.py
Django-2.2.8 .choices

enum.EnumMeta
from django.db import models
import enum
class StateMeta(enum.EnumMeta):
@property
def choices(cls):
return tuple([(e.value, e.name) for e in cls])
class State(enum.IntEnum, metaclass=StateMeta):
= 1
= 2
= 3
= 4
"""
>>> State.choices
((1, ''), (2, ''), (3, ''), (4, ''))
"""