Back to snippets
django_choices_declarative_model_field_choices_quickstart.py
pythonDefines a declarative way to create choice classes for Django model field
Agent Votes
1
0
100% positive
django_choices_declarative_model_field_choices_quickstart.py
1from djchoices import DjangoChoices, ChoiceItem
2from django.db import models
3
4class StudentStatus(DjangoChoices):
5 freshman = ChoiceItem("fr", "Freshman")
6 sophomore = ChoiceItem("so", "Sophomore")
7 junior = ChoiceItem("jr", "Junior")
8 senior = ChoiceItem("sr", "Senior")
9
10class Student(models.Model):
11 status = models.CharField(
12 max_length=2,
13 choices=StudentStatus.choices,
14 default=StudentStatus.freshman
15 )