Introduction
Forms are present on all websites. They serve the purpose of collecting data from the users. When creating a Form class in Django, one of the essential parts is defining the form’s fields. For some forms, we might use drop-downs, then we will have to provide choices for those fields. In this blog, we will see how that's done.
In this blog, we are discussing Django field choices. To read more about Django, check out this blog.
Field.choices
Django field choices are sequences consisting of iterables of exactly two items (e.g. [(A, B), (C, D) ...]) to use as choices for some field. If the choices are provided, they're enforced by model validation. The default form widget will be a select box with specified choices, not the standard text field.
The first element in each tuple is the actual value set on the model, and the second element is the human-readable name.
Let us see a code snippet for the Django field choices.
DAYS_OF_THE_WEEK = [
('SUN', 'Sunday),
('MON', 'Monday'),
('TUE', 'Tuesday’),
(‘WED', ‘Wednesday’),
('THURS', 'Thursday’),
('FRI', 'Friday’),
('SAT', 'Saturday’),
]
In the above code, the first element present in each tuple is the actual value set on the model, whereas the second element is the human-readable name.
Let us create Django field choices with the above days of the week in our Django project named FieldChoices. Assume there is a field to select the day of the week. The choices will be Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, and Saturday.
from django.db import models
# Specifying the choices
DAYS_OF_THE_WEEK = (
("SUN", "Sunday"),
("MON", "Monday"),
("TUE", "Tuesday"),
("WED", "Wednesday"),
("THURS", "Thursday"),
("FRI", "Friday"),
("SAT", "Saturday"),
)
# Declaring a Days Model
class DaysOfTheWeek(models.Model):
DaysOfWeek = models.CharField(
max_length = 20,
choices = DAYS_OF_THE_WEEK,
default = 'MON'
)

Output





