data-manager/models/user.py
2026-01-08 10:25:45 +08:00

29 lines
1.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from extensions import db # 从扩展模块导入初始化好的 SQLAlchemy 数据库实例
# 定义 User 类,继承自 db.Model使其成为一个数据库模型类对应数据库的一张表
class User(db.Model):
# 【表配置】显式指定在数据库中生成的表名为 "users"
__tablename__ = "users"
# 【字段定义】
# id字段名db.Integer整数类型primary_key=True设为主键通常会自动自增
id = db.Column(db.Integer, primary_key=True)
# username用户名db.String(64)最大长度64的字符串
# unique=True值必须唯一不可重复nullable=False不允许为空必填项
username = db.Column(db.String(64), unique=True, nullable=False)
# password密码db.String(128)最大长度128通常存加密后的哈希串
# nullable=False不允许为空
password = db.Column(db.String(128), nullable=False)
# 【实例方法:序列化】
# 将数据库对象转换为 Python 字典,方便在 Web 开发中返回 JSON 数据给前端
def to_dict(self):
return {
"id": self.id,
"username": self.username
# 注意:出于安全考虑,通常不会在 to_dict 中返回 password 字段
}