📝 Django

Choices in Django Models

P
Author
Pyland
📅
Published
30.06.2026
⏱️
Reading time
1 min
👁️
Views
75
🌿
Level
Medium

TextChoices is preferred — it provides autocompletion and avoids magic strings.

TextChoices / IntegerChoices

from django.db import models

class Task(models.Model):
    class Status(models.TextChoices):
        TODO = 'todo', 'To Do'
        IN_PROGRESS = 'in_progress', 'In Progress'
        DONE = 'done', 'Done'

    class Priority(models.IntegerChoices):
        LOW = 1, 'Low'
        MEDIUM = 2, 'Medium'
        HIGH = 3, 'High'

    status = models.CharField(
        max_length=20,
        choices=Status,
        default=Status.TODO,
    )
    priority = models.IntegerField(
        choices=Priority,
        default=Priority.MEDIUM,
    )

Using Choices

# Creating
task = Task.objects.create(status=Task.Status.TODO, priority=Task.Priority.HIGH)

# Filtering
Task.objects.filter(status=Task.Status.DONE)

# Human-readable value
task.get_status_display()  # 'To Do'
task.get_priority_display()  # 'High'

# Iterating over options
for value, label in Task.Status.choices:
    print(value, label)

In a Template

<span>{{ task.get_status_display }}</span>

<select name="status">
  {% for value, label in status_choices %}
    <option value="{{ value }}">{{ label }}</option>
  {% endfor %}
</select>

Old Style (without enum)

STATUS_CHOICES = [
    ('todo', 'To Do'),
    ('done', 'Done'),
]
status = models.CharField(choices=STATUS_CHOICES, max_length=20)

TextChoices is preferred — it provides autocompletion and avoids magic strings.

Your reaction to the article

💬 Comments (0)

🔐 Sign in to leave a comment
🚪 Login
💭

No comments yet

Be the first to share your opinion about this article!

🔗 Similar

Similar articles

Continue learning with these materials

📝

pytest-django: Testing Django

Охватываемые темы: Installation, @pytest.mark.djangodb, Fixtures, Testing views.

📅 30.06.2026 👁️ 138
📝

Django: Template Tags

Template tags are logic inside HTML. Unlike {{ variable }} which only outputs a value,...

📅 30.06.2026 👁️ 87
📝

Django: Static Files

Static files are CSS, JavaScript, images, and fonts. Django handles them in a specific way:...

📅 30.06.2026 👁️ 79

Did you like the article?

Subscribe to our updates and receive new articles first. Grow with PyLand!