Django课堂签到系统的models.py学生表教师表用户表课程表选课表签到表如何编写
时间: 2024-06-07 19:01:38 浏览: 124
基于安卓课堂签到签到系统,教师端+学生端,源代码-服务端-数据库
5星 · 资源好评率100%
下面是一个基本的Django课堂签到系统的models.py代码示例,包括学生表、教师表、用户表、课程表、选课表和签到表:
```
from django.db import models
from django.contrib.auth.models import User
# 学生表
class Student(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
student_id = models.CharField(max_length=20)
class_name = models.CharField(max_length=50)
major = models.CharField(max_length=50)
grade = models.IntegerField()
def __str__(self):
return self.user.username
# 教师表
class Teacher(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
teacher_id = models.CharField(max_length=20)
department = models.CharField(max_length=50)
def __str__(self):
return self.user.username
# 课程表
class Course(models.Model):
course_id = models.CharField(max_length=20)
course_name = models.CharField(max_length=50)
teacher = models.ForeignKey(Teacher, on_delete=models.CASCADE)
students = models.ManyToManyField(Student, through='Enrollment')
def __str__(self):
return self.course_name
# 选课表
class Enrollment(models.Model):
student = models.ForeignKey(Student, on_delete=models.CASCADE)
course = models.ForeignKey(Course, on_delete=models.CASCADE)
enroll_date = models.DateTimeField(auto_now_add=True)
def __str__(self):
return '{} - {}'.format(self.student.user.username, self.course.course_name)
# 签到表
class Attendance(models.Model):
student = models.ForeignKey(Student, on_delete=models.CASCADE)
course = models.ForeignKey(Course, on_delete=models.CASCADE)
attend_date = models.DateTimeField(auto_now_add=True)
def __str__(self):
return '{} - {}'.format(self.student.user.username, self.course.course_name)
```
在上述代码中,我们使用了Django提供的多种字段类型,如CharField、IntegerField、DateTimeField和ForeignKey等,来定义不同的数据表,并使用了ManyToManyField字段来定义了学生和课程之间的多对多关系。通过在选课表和签到表中使用ForeignKey字段,我们可以将学生和课程关联起来,并记录选课和签到的时间。最后,我们在每个模型类中定义了__str__方法,以便在后台管理界面中显示相关的信息。
阅读全文