- Import the necessary module from Django:
from django.db import models
- Define a class using IntegerChoices:
class StatusChoices(models.IntegerChoices):
DRAFT = 1, 'Draft'
PUBLISHED = 2, 'Published'
ARCHIVED = 3, 'Archived'
- Use the defined choices in a model field:
class YourModel(models.Model):
status = models.IntegerField(choices=StatusChoices.choices, default=StatusChoices.DRAFT)
- Access the choices in your code:
# Get the display value of a choice
print(StatusChoices(StatusChoices.PUBLISHED).label)
# Get the database value of a choice
print(StatusChoices(StatusChoices.PUBLISHED).value)