40 lines
1.3 KiB
Python
40 lines
1.3 KiB
Python
|
|
"""
|
||
|
|
Item 数据模型
|
||
|
|
"""
|
||
|
|
from tortoise.models import Model
|
||
|
|
from tortoise import fields
|
||
|
|
from datetime import datetime
|
||
|
|
|
||
|
|
class Item(Model):
|
||
|
|
"""物品模型"""
|
||
|
|
|
||
|
|
id = fields.IntField(pk=True, description="主键ID")
|
||
|
|
name = fields.CharField(max_length=100, description="名称")
|
||
|
|
description = fields.TextField(null=True, description="描述")
|
||
|
|
price = fields.DecimalField(max_digits=10, decimal_places=2, default=0.00, description="价格")
|
||
|
|
is_active = fields.BooleanField(default=True, description="是否激活")
|
||
|
|
|
||
|
|
# 关联用户(可选)
|
||
|
|
owner = fields.ForeignKeyField(
|
||
|
|
"models.User",
|
||
|
|
related_name="items",
|
||
|
|
null=True,
|
||
|
|
on_delete=fields.SET_NULL,
|
||
|
|
description="所有者"
|
||
|
|
)
|
||
|
|
|
||
|
|
# 时间戳
|
||
|
|
created_at = fields.DatetimeField(auto_now_add=True, description="创建时间")
|
||
|
|
updated_at = fields.DatetimeField(auto_now=True, description="更新时间")
|
||
|
|
|
||
|
|
def __str__(self):
|
||
|
|
return f"Item(id={self.id}, name={self.name}, price={self.price})"
|
||
|
|
|
||
|
|
class Meta:
|
||
|
|
table = "items"
|
||
|
|
table_description = "物品表"
|
||
|
|
ordering = ["-created_at"]
|
||
|
|
|
||
|
|
class PydanticMeta:
|
||
|
|
# Pydantic 模型配置
|
||
|
|
exclude = ["is_active"]
|