Junior — Middle
What does the IntegerChoices data type represent and what is it used for?
sobes.tech AI
Answer from AI
IntegerChoices is a convenient way to define a set of fixed integer constants with understandable names in Python, often used in Django for selecting values in models.
It allows you to set pairs (numeric value, human-readable name), which can be used, for example, for a model field with a limited set of options.
Example of usage in Django:
from django.db import models
class Status(models.IntegerChoices):
DRAFT = 1, 'Draft'
PUBLISHED = 2, 'Published'
ARCHIVED = 3, 'Archived'
class Article(models.Model):
title = models.CharField(max_length=100)
status = models.IntegerField(choices=Status.choices, default=Status.DRAFT)
Here, IntegerChoices helps make the code more readable and avoid magic numbers.