IntegerChoices django

  1. Import the necessary module from Django:
from django.db import models
  1. Define a class using IntegerChoices:
class StatusChoices(models.IntegerChoices):
    DRAFT = 1, 'Draft'
    PUBLISHED = 2, 'Published'
    ARCHIVED = 3, 'Archived'
  1. Use the defined choices in a model field:
class YourModel(models.Model):
    status = models.IntegerField(choices=StatusChoices.choices, default=StatusChoices.DRAFT)
  1. 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)