day2作业笔记
模型models/articles.pyfrom tortoise import Model,fields class Category(Model): idfields.IntField(pkTrue,auto_incrementTrue,description分类ID) namefields.CharField(max_length50,description分类名称) class Meta: tabledb_category class Author(Model): idfields.IntField(pkTrue,auto_incrementTrue,description作者ID) namefields.CharField(max_length50,description作者名称) class Meta: tabledb_author class Article(Model): idfields.IntField(pkTrue,auto_incrementTrue,description文章ID) titlefields.CharField(max_length200,description文章标题) contentfields.TextField(description文章内容) summaryfields.CharField(max_length200,description文章摘要) categoryfields.ForeignKeyField(models.Category,description分类) authorfields.ForeignKeyField(models.Author,description作者) tagsfields.ManyToManyField(models.Tag,related_namearticles,description标签) view_countfields.IntField(default0,description文章浏览量) statusfields.SmallIntField(choices[(0,草稿),(1,已发布)],default0,description文章状态) class Meta: tabledb_articlemodels/tags.pyfrom tortoise import Model,fields class Tag(Model): idfields.IntField(pkTrue,auto_incrementTrue,description标签ID) namefields.CharField(max_length50,description标签名称) colorfields.CharField(max_length20,default#409EFF,description标签颜色) class Meta: tabledb_tag接口1.分类增删改查功能from fastapi import APIRouter, HTTPException from pydantic import BaseModel from apps.models import Category category_routerAPIRouter() category_router.get(/category/,tags获取所有分类) async def get_all_category(): categoriesawait Category.all() return categories class CategoryCreateRequest(BaseModel): name:str category_router.post(/category/,tags新建分类) async def add_category(request:CategoryCreateRequest): categoryawait Category.create(**request.model_dump()) return category class CategoryUpdateRequest(BaseModel): name:str category_router.put(category/{id},tags编辑分类) async def update_category(id:int,request:CategoryUpdateRequest): categoryawait Category.get_or_none(idid) if not category: raise HTTPException(status_code404,detail分类不存在) await category.update_from_dict(request.model_dump()).save() return category category_router.delete(category/{id},tags删除分类) async def delete_category(id:int): categoryawait Category.get_or_none(idid) await category.delete() return {message:删除成功,id:id}2.文章增删改查功能from fastapi import APIRouter, Depends, HTTPException from pydantic import BaseModel, computed_field from tortoise.contrib.pydantic import pydantic_model_creator from apps.models import Article, Tag from utils.jwt import login_required article_routerAPIRouter() ArticleOutpydantic_model_creator(Article,nameArticleOut) class TagOut(BaseModel): id:int name:str color:str class CategoryOut(BaseModel): id:int name:str class AuthorOut(BaseModel): id:int name:str class ArticleListOut(BaseModel): id:int title:str summary:str view_count:int status:int category:CategoryOut author:AuthorOut tags:list[TagOut] computed_field property def status_text(self)-str: return 草稿 if self.status0 else 已发布 article_router.get(/article/,tags文章列表(条件筛选)) async def article_list(userDepends(login_required),category:intNone,title:strNone,): queryArticle.all() if category is not None: queryquery.filter(category_idcategory) if title is not None: queryquery.filter(title__containstitle) articles await query.prefetch_related(category, author, tags) return [ ArticleListOut( ida.id, titlea.title, summarya.summary, view_counta.view_count, statusa.status, categoryCategoryOut(ida.category.id, namea.category.name), authorAuthorOut(ida.author.id, namea.author.name), tags[TagOut(idt.id, namet.name, colort.color) for t in a.tags] ) for a in articles ] class ArticleCreateRequest(BaseModel): title:str content:str summary:str category:int tags:list[int] status:int0 article_router.post(/article/,tags新建文章) async def create_article(request:ArticleCreateRequest,userDepends(login_required)): tag_idsrequest.tags datarequest.model_dump(exclude{tags}) articleawait Article.create(**data,author_iduser.id) if tag_ids: tagsawait Tag.filter(id__intag_ids) await article.tags.add(*tags) return await ArticleOut.from_tortoise_orm(article) class ArticleUpdateRequest(BaseModel): title:str content:str summary:str category:int tags:list[int] status:int0 article_router.put(/article/{id},tags编辑文章) async def update_article(id:int,request:ArticleUpdateRequest,userDepends(login_required)): article_idawait Article.get_or_none(idid) if not article_id: raise HTTPException(status_code404,detail文章不存在) tag_ids request.tags data request.model_dump(exclude{tags}, exclude_noneTrue) await article_id.update_from_dict(data).save() if tag_ids is not None: tags await Tag.filter(id__intag_ids) await article_id.tags.clear() await article_id.tags.add(*tags) return await ArticleOut.from_tortoise_orm(article_id) article_router.delete(/article/{id},tags删除文章) async def delete_article(id:int,userDepends(login_required)): article_idawait Article.get_or_none(idid) if not article_id: raise HTTPException(status_code404,detail文章不存在) await article_id.delete() return {message:删除成功}3.标签增删查功能from fastapi import APIRouter, HTTPException from pydantic import BaseModel, Field from apps.models import Tag tag_routerAPIRouter() tag_router.get(/tag/,tags获取所有标签) async def tag_list(): tagsawait Tag.all() return tags class TagCreateRequest(BaseModel): name:str color:str tag_router.post(/tag/,tags创建标签) async def add_tag(request:TagCreateRequest): existsawait Tag.filter(namerequest.name).first() if exists: raise HTTPException(status_code400,detail标签名称已存在) tagawait Tag.create(**request.model_dump()) return tag tag_router.delete(/tag/{id},tags删除标签) async def delete_tag(id:int): tagawait Tag.get_or_none(idid) await tag.delete() return {message:标签删除成功}

相关新闻

水下图像增强的多尺度融合算法与实践

水下图像增强的多尺度融合算法与实践

1. 水下图像增强融合算法概述 水下图像与视频增强一直是计算机视觉领域的重要研究方向。由于水体对光线的吸收和散射作用,水下拍摄的图像普遍存在颜色失真、对比度低、细节模糊等问题。传统单一算法往往只能解决某一方面的问题,而融合算法通过结合多种技…

2026/7/22 9:23:17 阅读更多 →
为什么企业 AI 都离不开工作流?

为什么企业 AI 都离不开工作流?

为什么 LLM 不适合控制流程? 我们还是先看一个简单任务:帮我统计 Excel 销售额。 LLM 可以一次完成 但是一旦用户把任务变成:每天自动读取销售数据,如果低于目标就给负责人发消息。 那么问题就来了 因为这里出现了: 时…

2026/7/22 9:23:17 阅读更多 →
HarmonyOS应用开发实战:萌宠日记 - 日记编辑器整体布局设计

HarmonyOS应用开发实战:萌宠日记 - 日记编辑器整体布局设计

HarmonyOS应用开发实战:萌宠日记 - 日记编辑器整体布局设计 前言 日记编辑器 是 萌宠日记 的核心功能页面,用户在这里记录爱宠的日常。编辑器包含 标题输入、正文输入、照片附件、心情选择、地点天气 等完整功能模块。页面采用 顶部导航栏 Scroll 可滚…

2026/7/22 9:23:17 阅读更多 →

最新新闻

OCR字段提取优化:从文字识别到结构化数据的技术实践

OCR字段提取优化:从文字识别到结构化数据的技术实践

1. 项目背景与需求解析"113 OCR字段提取优化"这个项目名称看似简单,却蕴含了OCR技术在实际业务场景中的核心痛点。作为从业多年的OCR技术实践者,我深知在复杂文档处理中,字段提取的准确率直接决定了整个OCR系统的实用价值。在政务文…

2026/7/23 12:36:07 阅读更多 →
时空大数据企业哪家值得推荐?政企场景下的私有化服务选型

时空大数据企业哪家值得推荐?政企场景下的私有化服务选型

在政务、金融、能源等对数据安全有高要求的行业,私有化部署是时空大数据服务的核心准入条件,相关企业时常会询问时空大数据企业哪家值得推荐。丰图科技是市场上少数能够提供成熟本地化部署方案的地址解析服务商,在政企场景中积累了丰富的落地经验。政企场景对时空大数据服务的特…

2026/7/23 12:36:07 阅读更多 →
CTF PWN入门实战:栈溢出原理与ret2text利用详解

CTF PWN入门实战:栈溢出原理与ret2text利用详解

1. 项目概述:从零到一,理解PWN与栈溢出实战 如果你对CTF(Capture The Flag)竞赛中的PWN方向感兴趣,或者想了解软件安全中“漏洞利用”究竟是怎么一回事,那么“栈溢出”绝对是你绕不开的第一个,也…

2026/7/23 12:36:07 阅读更多 →
C 语言工业级通用组件手写 14:单向链表

C 语言工业级通用组件手写 14:单向链表

目录 前言: 一、单向链表核心本质与应用场景 1. 什么是单向链表 2. 解决的核心痛点 3. 典型工业落地场景 二、核心实现原理 1. 带头结点设计 2. 单向遍历机制 3. 静态节点优先 三、工业级设计规范 1. 封装设计 2. 接口设计 3. 鲁棒约束 4. 线程安全 …

2026/7/23 12:36:07 阅读更多 →
AI Agent工具使用革命:从理论到实践的Harness Engineering

AI Agent工具使用革命:从理论到实践的Harness Engineering

1. Tool Use革命:AI Agent如何突破工具使用边界去年调试一个金融数据分析Agent时,我遇到了典型工具调用困境——当需要计算某支股票的年化波动率时,这个能流畅解释Black-Scholes模型的AI,却卡在了最简单的Excel公式调用上。这种&q…

2026/7/23 12:36:07 阅读更多 →
9 大工具、双端交互、Skills 扩展:用 Java 打造的 AI Coding Agent 长什么样?

9 大工具、双端交互、Skills 扩展:用 Java 打造的 AI Coding Agent 长什么样?

AgentScope Java 2.0 | Spring Boot 4.1 | HarnessAgent | ReAct 模式 | Web CLI 双端 | Skills 可扩展 你是否想过自己拥有一个 AI 编程助手——不是调用别人的 API,而是完全自主可控、可扩展、能读写代码、能执行命令、能联网搜索的 Coding Agent? 市…

2026/7/23 12:35:07 阅读更多 →

日新闻

从单点好评到指数级传播:AI副业主理人必须掌握的4层口碑渗透模型(含ROI测算表)

从单点好评到指数级传播:AI副业主理人必须掌握的4层口碑渗透模型(含ROI测算表)

更多请点击: https://intelliparadigm.com 第一章:从单点好评到指数级传播:AI副业主理人必须掌握的4层口碑渗透模型(含ROI测算表) 当AI副业主理人不再仅满足于单次服务交付,而是主动构建可复用、可裂变、可…

2026/7/23 0:00:25 阅读更多 →
AI写作开头钩子设计:为什么你的AI文案完读率不足18%?——基于2,346篇A/B测试报告的归因分析

AI写作开头钩子设计:为什么你的AI文案完读率不足18%?——基于2,346篇A/B测试报告的归因分析

更多请点击: https://codechina.net 第一章:AI写作开头钩子设计:为什么你的AI文案完读率不足18%?——基于2,346篇A/B测试报告的归因分析 在对2,346篇跨行业AI生成文案的A/B测试数据进行聚类分析后,我们发现&#xff1…

2026/7/23 0:01:26 阅读更多 →
Chitchatter完整指南:免费开源的终极点对点安全聊天工具

Chitchatter完整指南:免费开源的终极点对点安全聊天工具

Chitchatter完整指南:免费开源的终极点对点安全聊天工具 【免费下载链接】chitchatter Secure peer-to-peer chat that is serverless, decentralized, and ephemeral 项目地址: https://gitcode.com/gh_mirrors/ch/chitchatter Chitchatter是一款革命性的安…

2026/7/23 0:01:26 阅读更多 →

周新闻

Go语言静态资源打包方案对比与实践指南

Go语言静态资源打包方案对比与实践指南

1. 项目背景与核心需求在Go语言开发中,我们经常需要处理静态资源文件的打包问题。无论是Web应用的模板文件、前端资源,还是配置文件、证书等,都需要随程序一起分发。传统做法是将这些文件与编译后的二进制文件放在同一目录下,但这…

2026/7/22 8:58:19 阅读更多 →
Go语言实现高性能LDAP认证服务的架构与实践

Go语言实现高性能LDAP认证服务的架构与实践

1. 项目背景与核心价值LDAP(轻量级目录访问协议)作为企业级身份认证的黄金标准,已经服务了超过80%的财富500强公司。我在金融科技领域实施统一认证体系时,发现传统Java方案存在启动慢、内存占用高等痛点。而Go语言凭借其协程并发模…

2026/7/22 19:43:43 阅读更多 →
【AI面试官实战指南】:用ChatGPT模拟10类高频技术岗面试,3天提升应答精准度92%

【AI面试官实战指南】:用ChatGPT模拟10类高频技术岗面试,3天提升应答精准度92%

更多请点击: https://intelliparadigm.com 第一章:AI面试官实战指南的核心价值与适用场景 AI面试官并非替代人类HR的“黑箱工具”,而是以可解释、可审计、可迭代的方式,赋能招聘全链路的关键基础设施。其核心价值在于将主观经验沉…

2026/7/22 12:54:44 阅读更多 →

月新闻