12. 开发学生成绩模块

156 字约 1 分钟读完163 次阅读更新于 2026/5/3

SQL

CREATE TABLE `grade` (
  `id` int NOT NULL AUTO_INCREMENT COMMENT 'ID',
  `student_id` int DEFAULT NULL COMMENT '学生ID',
  `course_id` int DEFAULT NULL COMMENT '课程ID',
  `score` int DEFAULT NULL COMMENT '成绩',
  `ispass` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '是否及格',
  `time` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '创建时间',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='成绩信息';

学生选课的查询接口

# 查询所有数据
@router.get('/selectAll')
async def select_all(studentId :int = 0, status: str = ""):
    query = StudentCourse.all().prefetch_related("course")
    if studentId > 0:
        query = query.filter(student__id=studentId)
    if status != "":
        query = query.filter(status=status)
    student_course_list = await query
    student_course_dict_list = [
        {
            **StudentCoursePydantic.model_validate(student_course).model_dump(),
            "courseId": student_course.course.id if student_course.course else None,
            "courseName": student_course.course.name if student_course.course else None
        }
        for student_course in student_course_list
    ]
    return Result.success(student_course_dict_list)


给学生加上学分的字段

CREATE TABLE `student` (
  `id` int NOT NULL AUTO_INCREMENT COMMENT 'ID',
  `username` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '账号',
  `password` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '密码',
  `name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '名称',
  `avatar` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '头像',
  `role` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '角色',
  `clazz_id` int DEFAULT NULL COMMENT '班级',
  `score` int DEFAULT NULL COMMENT '学分',
  PRIMARY KEY (`id`) USING BTREE,
  UNIQUE KEY `username` (`username`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci ROW_FORMAT=DYNAMIC COMMENT='学生信息';

注意登录的返回模型 加上 score 字段

class Account(BaseModel):
    model_config = ConfigDict(from_attributes=True)

    id: int | None = None
    username: str | None = None
    password: str | None = None
    newPassword: str | None = None
    role: str | None = None
    name: str | None = None
    avatar: str | None = None
    clazzId: int | None = None
    majorId: int | None = None
    score: int | None = None

退课 更新状态

# 更新
@router.put("/update")
async def add(student_course_create_pydantic: StudentCourseCreatePydantic):
    if student_course_create_pydantic.id is None:
        raise CustomException("缺少参数ID")
    if student_course_create_pydantic.status == '已退':
        grade = await (Grade.filter(student_id=student_course_create_pydantic.student_id)
                       .filter(course_id=student_course_create_pydantic.course_id).first())
        if grade is not None:
            raise CustomException('当前课程已打分,无法退课')
    if student_course_create_pydantic.checkStatus == '通过':
        student_course_create_pydantic.status = '已选'
    elif student_course_create_pydantic.checkStatus == '拒绝':
        student_course_create_pydantic.status = '未选中'
    # 将参数转换成 字典数据
    update_data = student_course_create_pydantic.model_dump(exclude_unset=True, exclude={"id"})
    await StudentCourse.filter(id=student_course_create_pydantic.id).update(
        **update_data)  # no=xxx,name=xxx,college=xxx where id = xxx
    return Result.success()

学生成绩后台接口

学生成绩页面