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 阅读更多 →

最新新闻

C2000 DCSM与MEM_CFG寄存器详解:内存安全与多核访问配置实战

C2000 DCSM与MEM_CFG寄存器详解:内存安全与多核访问配置实战

1. 项目概述与核心价值 如果你正在使用TI的C2000系列微控制器,尤其是TMS320F2837xS这类双核或者带CLA(控制律加速器)的型号,那么你迟早会碰到一个绕不开的“硬骨头”——如何安全、有效地管理你的Flash和RAM。这不仅仅是把代码和数…

2026/7/22 10:07:34 阅读更多 →
从信号干扰到精准定位:DXM-2000C 助力油田通信安全防线

从信号干扰到精准定位:DXM-2000C 助力油田通信安全防线

在石油能源行业,海上钻井平台的精细化作业、绵延千里的输油管道全天候监控,都高度依赖稳定可靠的无线通信系统,是保障生产安全、调度顺畅与作业高效的重要基石。随着油田智能化改造推进,现场无线终端、传感设备、通信设备数量持续…

2026/7/22 10:07:34 阅读更多 →
2026小程序开发工具哪家好?拖拽式搭建了解一下!

2026小程序开发工具哪家好?拖拽式搭建了解一下!

2026小程序开发工具哪家好?拖拽式搭建了解一下!先看一组数据:截至2026年3月,小程序整体月活用户规模已经达到10.21亿,其中微信小程序月活9.73亿、支付宝小程序6.44亿、抖音小程序2.73亿。小程序早已从“拉新工具”变成…

2026/7/22 10:07:34 阅读更多 →
从“轻量级虚拟机”到理解容器本质

从“轻量级虚拟机”到理解容器本质

从“轻量级虚拟机”到理解容器本质 今天正式开始了工程化部署的学习,目标是从最底层的容器技术开始,逐步掌握 Docker CI/CD 的完整流程。第一天的内容看似基础,但深入进去才发现,很多以前模模糊糊的概念一下子清晰了。这篇文章记…

2026/7/22 10:07:34 阅读更多 →
TradingAgents-CN多智能体金融交易框架:7个高级调试技巧与实战解决方案

TradingAgents-CN多智能体金融交易框架:7个高级调试技巧与实战解决方案

TradingAgents-CN多智能体金融交易框架:7个高级调试技巧与实战解决方案 【免费下载链接】TradingAgents-CN 基于多智能体LLM的中文金融交易框架 - TradingAgents中文增强版 项目地址: https://gitcode.com/GitHub_Trending/tr/TradingAgents-CN TradingAgent…

2026/7/22 10:07:34 阅读更多 →
互联网大厂 Java 求职面试:Spring Boot、Kafka、微服务的探讨

互联网大厂 Java 求职面试:Spring Boot、Kafka、微服务的探讨

互联网大厂 Java 求职面试(涵盖 Spring Boot、Kafka、微服务等技术) 在这篇文章中,我们将探讨一次互联网大厂的 Java 求职面试。面试官是一位严肃的工程师,而候选人燕双非则是一位幽默风趣的程序员。整个面试过程围绕着多个技术点…

2026/7/22 10:06:34 阅读更多 →

日新闻

TI DSP系统配置模块SYSCFG详解:中断机制与主设备优先级配置实战

TI DSP系统配置模块SYSCFG详解:中断机制与主设备优先级配置实战

1. 项目概述与SYSCFG模块的核心价值在嵌入式系统,尤其是像TI C6000系列这样的高性能DSP开发中,我们常常会与芯片手册里那些密密麻麻的寄存器打交道。很多开发者可能更关注算法实现、内存优化或者外设驱动,但对于一个稳定、高效的系统而言&…

2026/7/22 0:00:26 阅读更多 →
微信Server酱:高到达率的应急通知方案实践

微信Server酱:高到达率的应急通知方案实践

1. 为什么我们需要"最次"的通知方案? 在数字化协作环境中,消息通知系统的重要性不言而喻明。但现实情况是,企业级通知方案往往需要复杂的API对接(如企业微信、钉钉、飞书),个人开发者的小项目又经…

2026/7/22 0:00:26 阅读更多 →
甲方要的“简洁“PPT,到底是简洁还是省事?

甲方要的“简洁“PPT,到底是简洁还是省事?

甲方说"简洁一点",乙方听到的是"少做几页"。甲方说"不要太复杂",乙方理解成"别放图表了"。结果交过去,甲方说"我说的简洁不是这个意思"。"简洁"这个词在PPT语境里,是…

2026/7/22 0:00:26 阅读更多 →

周新闻

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

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

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

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

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

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

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

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

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

2026/7/21 8:25:39 阅读更多 →

月新闻