TIL

Django Enumeration type

Django provides enumeration types that you can subclass to define choices in a concise way. The diff below show how you would introduce it in your code base:

 class Category(models.Model):
     """Category model."""
-    PRIMARY = 10
-    SECONDARY = 20
-    KIND_CHOICES = (
-        (PRIMARY, _("Primary")),
-        (SECONDARY, _("Secondary")),
-    )
+    class Kind(models.IntegerChoices):
+        PRIMARY = 10, _("Primary")
+        SECONDARY = 20, _("Secondary")
+
     title = models.CharField(_("title"), max_length=100)
     slug = models.SlugField(_("slug"), unique=True)
     active = models.BooleanField(
         default=True, help_text="Active categories are displayed on the sidebar"
     )
-    kind = models.IntegerField(choices=KIND_CHOICES, default=SECONDARY)
+    kind = models.IntegerField(choices=Kind.choices, default=Kind.SECONDARY)