1Will create a select box for this in the model form. Use can select from the
2options.The first element in each tuple is the actual value to be set on the model,
3and the second element is the human-readable name. For example:
4YEAR_IN_SCHOOL_CHOICES = [
5 (FRESHMAN, 'Freshman'),
6 (SOPHOMORE, 'Sophomore'),
7 (JUNIOR, 'Junior'),
8 (SENIOR, 'Senior'),
9 (GRADUATE, 'Graduate'),
10 ]
11 year_in_school = models.CharField(
12 max_length=2,
13 choices=YEAR_IN_SCHOOL_CHOICES,
14 default=FRESHMAN,
15 )
1from django.db import models
2
3class Author(models.Model):
4 first_name = models.CharField(max_length=64)
5 last_name = models.CharField(max_length=64)
6
7 def __str__(self):
8 return '{} {}'.format(self.first_name, self.last_name)
9
10
11class Book(models.Model):
12 author = models.ForeignKey('books.Author', related_name='books', on_delete=models.CASCADE)
13 title = models.CharField(max_length=128)
14 status = models.CharField(
15 max_length=32,
16 choices=[], # some list of choices
17 )
18
19 def __str__(self):
20 return '{}: {}'.format(self.author, self.title)