Files

59 lines
2.2 KiB
Python
Raw Permalink Normal View History

2026-09-21 19:14:15 +08:00
# scheme/student.py
from pydantic import BaseModel, Field,model_validator
from datetime import datetime, date
from typing import Optional
# ---------- 请求体模型 ----------
class StudentCreate(BaseModel):
stu_id: int = Field(..., ge = 1)
class_id: int = Field(..., ge = 1)
advisor_id: int = Field(..., ge=1, description="顾问老师id,对应 advisors.advisor_id")
stu_name: str = Field(..., min_length=1, max_length=10)
native_place: str = Field(..., min_length=1, max_length=30)
graduate_school: str = Field(..., min_length=1, max_length=50)
education: str = Field(..., min_length=1, max_length=10)
major: str = Field(..., min_length=1, max_length=20)
age: int = Field(..., ge = 18, le =40)
gender: str = Field(..., min_length=1, max_length=20)
graduate_time:date= Field(...)
enroll_time:date= Field(...)
# @model_validator(mode="after")
# def check_time_order(self):
# enroll = self.enroll_time
# graduate = self.graduate_time
#
# # 两个时间都传入时才校验
# if enroll and graduate:
# if enroll >= graduate:
# raise ValueError("入学时间必须早于毕业时间")
class StudentUpdate(BaseModel):
class_id: Optional[int] = Field(None)
advisor_id: Optional[int] = Field(None, ge=1)
stu_name: Optional[str] = Field(None, min_length=1, max_length=10)
native_place: Optional[str] = Field(None, min_length=1, max_length=30)
graduate_school: Optional[str] = Field(None, min_length=1, max_length=50)
education: Optional[str] = Field(None, min_length=1, max_length=10)
major: Optional[str] = Field(None, min_length=1, max_length=20)
age: Optional[int] = Field(None, ge = 18, le =40)
gender: Optional[str] = Field(None, min_length=1, max_length=20)
# ----------响应体模型-------------
class StudentResponse(BaseModel):
stu_id: int
class_id: int # 数据库非空,这里必须是 int
stu_name: str
native_place: str
graduate_school: Optional[str]
education: str
major: Optional[str]
# 敏感字段已在此模型中脱敏
class Config:
from_attributes = True #支持orm对象转换,让pydantic可以读取ORM数据库对象(SQLAlchemy模型)