Django Choices with EnumsΒΆ
from enum import Enum
from django.db import models
class HamsterStatus(Enum):
MANUFACTURED = "manufactured"
ASSEMBLED = "assembled"
PACKAGED = "packaged"
SHIPPED = "shipped"
@classmethod
def choices(cls):
return [(key.value, key.name.replace('_', ' ').title()) for key in cls]
class Hamster(models.Model):
name = models.CharField(max_length=100)
status = models.CharField(max_length=20, choices=HamsterStatus.choices())
def __str__(self):
return self.name