update. 更新文章接口
This commit is contained in:
+31
-30
@@ -2,7 +2,6 @@ from datetime import datetime
|
||||
from typing import Optional, List, Generic, T
|
||||
|
||||
from ninja import Schema
|
||||
from pydantic import Field
|
||||
|
||||
|
||||
# --- 假设的关联模型 Schema ---
|
||||
@@ -16,47 +15,49 @@ class SectionSchema(Schema):
|
||||
title: str # 假设 MainMenu 有 name 字段
|
||||
|
||||
|
||||
class AttachmentSchema(Schema):
|
||||
class ArticleAttachmentOut(Schema):
|
||||
id: int
|
||||
file: str # 返回文件 URL 路径,如 "articles/attachments/2026/04/03/file.pdf"
|
||||
file: str
|
||||
|
||||
|
||||
class PaginatedResponseSchema(Schema, Generic[T]):
|
||||
items: List[T]
|
||||
total: int
|
||||
page: int
|
||||
per_page: int
|
||||
total_pages: int
|
||||
|
||||
|
||||
# --- Article 主 Schema ---
|
||||
class ArticleSchema(Schema):
|
||||
class ArticleListOut(Schema):
|
||||
id: int
|
||||
title: str
|
||||
content: str
|
||||
cover: Optional[str] = None # 图片 URL 路径
|
||||
|
||||
section: SectionSchema
|
||||
|
||||
author: AuthorSchema
|
||||
|
||||
cover: Optional[str] = None
|
||||
is_contribution: bool
|
||||
is_published: bool
|
||||
created_at: datetime
|
||||
updated_at: datetime
|
||||
|
||||
# 关联字段
|
||||
author: Optional[AuthorSchema] = None
|
||||
section: Optional[SectionSchema] = None
|
||||
attachments: List[AttachmentSchema] = Field(default_factory=list)
|
||||
|
||||
|
||||
# --- 创建文章用的 Schema (不含只读字段) ---
|
||||
class ArticleCreateSchema(Schema):
|
||||
class ArticleDetailOut(Schema):
|
||||
id: int
|
||||
title: str
|
||||
section_id: Optional[int] = None # 外键通过 ID 传递
|
||||
|
||||
section: SectionSchema
|
||||
|
||||
author: AuthorSchema
|
||||
|
||||
content: str
|
||||
cover: Optional[str] = None
|
||||
|
||||
is_contribution: bool
|
||||
is_published: bool
|
||||
|
||||
attachments: List[ArticleAttachmentOut] = []
|
||||
|
||||
|
||||
class ArticleCreate(Schema):
|
||||
title: str
|
||||
section_id: Optional[int] = None
|
||||
author_id: Optional[int] = None
|
||||
content: str
|
||||
cover: Optional[str] = None # 注意:文件上传通常需单独处理,此处仅为文本路径示例
|
||||
is_contribution: bool = False
|
||||
is_published: bool = True
|
||||
|
||||
|
||||
# --- 更新文章用的 Schema ---
|
||||
class ArticleUpdateSchema(ArticleCreateSchema):
|
||||
title: Optional[str] = None
|
||||
content: Optional[str] = None
|
||||
class ArticleUpdate(ArticleCreate):
|
||||
pass
|
||||
|
||||
Reference in New Issue
Block a user