104 lines
2.2 KiB
Python
104 lines
2.2 KiB
Python
from ninja import Schema
|
|
from datetime import datetime
|
|
from typing import Optional, List
|
|
|
|
|
|
class LinkCategorySchema(Schema):
|
|
id: int
|
|
name: str
|
|
code: str
|
|
order: int
|
|
is_active: bool
|
|
created_at: Optional[datetime] = None
|
|
|
|
|
|
class FriendLinkSchema(Schema):
|
|
id: int
|
|
category_id: int
|
|
name: str
|
|
url: str
|
|
order: int
|
|
is_active: bool
|
|
created_at: Optional[datetime] = None
|
|
|
|
|
|
class LinkCategoryCreateSchema(Schema):
|
|
name: str
|
|
code: str
|
|
order: int = 0
|
|
is_active: bool = True
|
|
|
|
|
|
class FriendLinkCreateSchema(Schema):
|
|
category_id: int
|
|
name: str
|
|
url: str
|
|
order: int = 0
|
|
is_active: bool = True
|
|
|
|
|
|
# ================= 响应模式 (Output) =================
|
|
class PictureLinkSchema(Schema):
|
|
id: int
|
|
name: str
|
|
picture: str # 这里返回的是图片路径字符串
|
|
url: str
|
|
order: int
|
|
is_active: bool
|
|
created_at: datetime
|
|
updated_at: datetime
|
|
|
|
class Config:
|
|
# 如果你的 Model 字段和 Schema 字段名一致,通常不需要 orm_mode
|
|
# 但为了保险,或者如果你直接返回 Model 实例,建议开启
|
|
from_attributes = True
|
|
|
|
|
|
# ================= 请求模式 (Input) =================
|
|
class PictureLinkCreateSchema(Schema):
|
|
name: str
|
|
picture: str
|
|
url: str
|
|
order: int = 0
|
|
is_active: bool = True
|
|
|
|
|
|
class PictureLinkUpdateSchema(Schema):
|
|
name: Optional[str] = None
|
|
picture: Optional[str] = None
|
|
url: Optional[str] = None
|
|
order: Optional[int] = None
|
|
is_active: Optional[bool] = None
|
|
|
|
|
|
class MainMenuBase(Schema):
|
|
"""基础菜单字段"""
|
|
title: str
|
|
code: str
|
|
url: str = ""
|
|
is_external: bool = False
|
|
is_carousel: bool = False
|
|
order: int = 0
|
|
visible: bool = True
|
|
|
|
|
|
class MainMenuCreate(MainMenuBase):
|
|
"""创建时的 Schema"""
|
|
parent_id: Optional[int] = None
|
|
|
|
|
|
class MainMenuUpdate(MainMenuBase):
|
|
"""更新时的 Schema"""
|
|
parent_id: Optional[int] = None
|
|
|
|
|
|
class MainMenuOut(MainMenuBase):
|
|
"""输出用的 Schema,包含 ID 和递归的子菜单"""
|
|
id: int
|
|
parent_id: Optional[int] = None
|
|
children: List['MainMenuOut'] = [] # 递归引用自身
|
|
|
|
|
|
# 告诉 Pydantic 如何处理递归模型
|
|
MainMenuOut.model_rebuild()
|