Choices
buraq.utils.choices provides Django-style enum base classes for defining field choices with labels.
TextChoices
Section titled “TextChoices”from buraq.utils.choices import TextChoices
class Status(TextChoices): DRAFT = "draft", "Draft" PUBLISHED = "published", "Published" ARCHIVED = "archived", "Archived"Use in a model field:
from sqlalchemy import Column, Stringfrom buraq.orm.base import Model
class Post(Model): status = Column(String, default=Status.DRAFT)IntegerChoices
Section titled “IntegerChoices”from buraq.utils.choices import IntegerChoices
class Priority(IntegerChoices): LOW = 1, "Low" MEDIUM = 2, "Medium" HIGH = 3, "High"Class properties
Section titled “Class properties”| Property | Returns |
|---|---|
Status.choices |
[("draft", "Draft"), ("published", "Published"), ...] |
Status.labels |
["Draft", "Published", "Archived"] |
Status.values |
["draft", "published", "archived"] |
Status.names |
["DRAFT", "PUBLISHED", "ARCHIVED"] |
Using with form fields
Section titled “Using with form fields”from buraq.forms import ChoiceField
class PostForm(Form): status = ChoiceField(choices=Status.choices)Comparing values
Section titled “Comparing values”Because TextChoices inherits from str and IntegerChoices from int, members compare equal to their raw values:
Status.DRAFT == "draft" # TruePriority.HIGH == 3 # True