django展示choice字段

class Student(models.Model):

sex_choices = (
(0, "女"),
(1, "男"),
(2, "保密"),
)
name = models.CharField(max_length=32, unique=True, verbose_name="姓名")
age = models.SmallIntegerField(verbose_name="年龄", default=18) # 年龄
sex = models.SmallIntegerField(choices=sex_choices)
birthday = models.DateField()

# 一对多的关系:在数据库创建一个关联字段:clas_id
clas = models.ForeignKey(to="Clas",related_name="student_list",on_delete=models.CASCADE)

# 一对一的关系:建立关联字段,在数据库中生成关联字段:stu_detail_id
stu_detail = models.OneToOneField("StudentDetail",related_name="stu",on_delete=models.CASCADE)

# 多对多的关系:创建第三张关系表
courses = models.ManyToManyField("Course",related_name="students", db_table="db_student2course")

class Meta:
db_table = "db_student"


def __str__(self):
return self.name
<thead>
                            <tr>
                                <th>序号</th>
                                <th>姓名</th>
                                <th>年龄</th>
                                <th>性别</th>
                                <th>生日</th>
                                <th>班级</th>
                                <th>选修课程</th>
                            </tr>
                            </thead>
                            <tbody>
                            {% for student in student_list %}
                                <tr>
                                    <td>{{ forloop.counter }}</td>
                                    <td>{{ student.name }}</td>
                                    <td>{{ student.age }}</td>
                                    <td>{{ student.get_sex_display }}</td>
                                    <td>{{ student.birthday|date:'Y-m-d' }}</td>
                                    <td>{{ student.clas.name }}</td>
                                    <td>
                                        {% for course in student.courses.all %}
                                            <button>{{ course.title }}</button>
                                        {% endfor %}
                                    </td>
                                </tr>
                            {% endfor %}
                            </tbody>

 

posted @ 2021-12-24 17:30  wslfw  阅读(187)  评论(0)    收藏  举报