How to translate Enum string in django?

Viewed 445

So I have a model that has a status field, for that I've created an Enum class, like that:

class Status(Enum):
    PENDING_APPROVAL = "PENDING-APPROVAL"
    PLANNED = "PLANNED"
    APPROVED = "APPROVED"
    CHANGED_PLAN = "CHANGED-PLAN"
    COMPLETED = "COMPLETED"
    CANCELED = "CANCELED"

And then added this class as a choice for the model:

status = models.CharField(
        choices=[(tag.value, tag.name) for tag in Status],
        max_length=20,
        verbose_name=pgettext("Order", "Status"),
        default=Status.PENDING_APPROVAL.value,
    )

All works fine, but the problem is that once I click on my dropdown to pick a choice from my Enum class, I want for my choices to be translated to the other language of my site.

I cannot find how to translate my Enum's class choices to the language that I need.

1 Answers

Use standard models.TextChoices which can optionally accepts translation too:

from django.utils.translation import gettext_lazy as _

class Status(models.TextChoices):
    APPROVED = 'APPROVED', _('Approved')
    CANCELED = 'CANCELED', _('Canceled')
Related