171 lines
4.2 KiB
Python
Raw Normal View History

2025-02-13 02:27:44 +08:00
# _*_ coding : UTF-8 _*_
# @Time : 2025/01/18 03:20
# @UpdateTime : 2025/01/18 03:20
# @Author : sonder
# @File : user.py
# @Software : PyCharm
# @Comment : 本程序
from tortoise import fields
from models.common import BaseModel
class User(BaseModel):
"""
用户表模型
"""
username = fields.CharField(
max_length=255,
description="用户名",
source_field="username" # 映射到数据库字段 username
)
"""
用户名
- 最大长度为 255 个字符
- 映射到数据库字段 username
"""
password = fields.CharField(
max_length=255,
description="密码",
source_field="password" # 映射到数据库字段 password
)
"""
密码
- 存储加密后的密码
- 最大长度为 255 个字符
- 映射到数据库字段 password
"""
email = fields.CharField(
max_length=255,
null=True,
description="邮箱",
source_field="email" # 映射到数据库字段 email
)
"""
邮箱
- 最大长度为 255 个字符
- 允许为空
- 映射到数据库字段 email
"""
phone = fields.CharField(
max_length=30,
null=True,
description="手机号",
source_field="phone" # 映射到数据库字段 phone
)
"""
手机号
- 最大长度为 30 个字符
- 允许为空
- 映射到数据库字段 phone
"""
nickname = fields.CharField(
max_length=255,
null=True,
description="昵称",
source_field="nickname" # 映射到数据库字段 nickname
)
"""
昵称
- 最大长度为 255 个字符
- 允许为空
- 映射到数据库字段 nickname
"""
avatar = fields.CharField(
max_length=255,
null=True,
description="头像",
source_field="avatar" # 映射到数据库字段 avatar
)
"""
头像
- 最大长度为 255 个字符
- 允许为空
- 映射到数据库字段 avatar
"""
gender = fields.SmallIntField(
default=0,
description="性别1男0女",
source_field="gender" # 映射到数据库字段 gender
)
"""
性别
- 1
- 0
- 默认为 0
- 映射到数据库字段 gender
"""
status = fields.SmallIntField(
default=1,
description="用户状态1启用0禁用",
source_field="status" # 映射到数据库字段 status
)
"""
用户状态
- 1启用
- 0禁用
- 默认为 1
- 映射到数据库字段 status
"""
department = fields.ForeignKeyField(
"models.Department",
related_name="users",
null=True,
description="所属部门",
source_field="department_id" # 映射到数据库字段 department_id
)
"""
所属部门
- 外键关联到 Department
- 如果为 null则表示用户未分配部门
- 映射到数据库字段 department_id
"""
roles = fields.ManyToManyField(
"models.Role",
related_name="users",
through="user_role",
description="用户角色"
)
"""
用户角色
- 多对多关系表示用户拥有的角色
- 通过中间表 `user_role` 关联角色表
"""
class Meta:
table = "user" # 数据库表名
table_description = "用户表" # 表描述
ordering = ["-create_time"] # 默认按创建时间倒序排序
class UserRole(BaseModel):
"""
用户角色中间表
"""
user = fields.ForeignKeyField(
"models.User",
related_name="user_roles",
source_field="user_id" # 映射到数据库字段 user_id
)
role = fields.ForeignKeyField(
"models.Role",
related_name="user_roles",
source_field="role_id" # 映射到数据库字段 role_id
)
class Meta:
table = "user_role" # 数据库表名
table_description = "用户角色中间表" # 表描述
unique_together = (("user_id", "role_id"),) # 唯一约束,防止重复分配