13.评论模块——peewee创建模型、tornado-peewee-async查询增加接口

2023-12-08 05:59

本文主要是介绍13.评论模块——peewee创建模型、tornado-peewee-async查询增加接口,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1.模型建立与数据初始化

peewee创建模型:多表关系

# 创建评论表
class CommentModel(BaseModel):id = CharField(primary_key = True)content = CharField(verbose_name='内容')topic = ForeignKeyField(TopicModel, backref='comments')user = ForeignKeyField(UserModel, backref='comments')class Meta:table_name = 't_comments'

sql添加内容

INSERT INTO `t_comment` (`id`, `create_time`, `content`, `topic_id`, `user_id`) VALUES ('5de0e0e05dd0a380dbc1acbf', '2050-01-01 00:00:00', '挺好看的嘛', '5de0def05dd0a380dbc1acbb', '5ddf83c8b78aa66cfb5b025e');
INSERT INTO `t_comment` (`id`, `create_time`, `content`, `topic_id`, `user_id`) VALUES ('5de0e0f05dd0a380dbc1acc0', '2050-01-01 00:00:00', '大晚上,让我看这个!', '5de0dfdf5dd0a380dbc1acbd', '5ddf83c8b78aa66cfb5b025e');
INSERT INTO `t_comment` (`id`, `create_time`, `content`, `topic_id`, `user_id`) VALUES ('5de0e0f85dd0a380dbc1acc1', '2050-01-01 00:00:00', '真的是,饿到不行', '5de0dfdf5dd0a380dbc1acbd', '5ddf83c8b78aa66cfb5b025e');
INSERT INTO `t_comment` (`id`, `create_time`, `content`, `topic_id`, `user_id`) VALUES ('5de0e10d5dd0a380dbc1acc3', '2050-01-01 00:00:00', '王哥,', '5de0de315dd0a380dbc1acb9', '5ddf83c8b78aa66cfb5b025e');
INSERT INTO `t_comment` (`id`, `create_time`, `content`, `topic_id`, `user_id`) VALUES ('5de0e1205dd0a380dbc1acc4', '2050-01-01 00:00:00', '笑死我,继承我的花呗?', '5de0dd105dd0a380dbc1acb5', '5ddf83c8b78aa66cfb5b025e');
INSERT INTO `t_comment` (`id`, `create_time`, `content`, `topic_id`, `user_id`) VALUES ('5de0e1985dd0a380dbc1acca', '2050-01-01 00:00:00', '666,大晚上有点东西啊', '5de0dfdf5dd0a380dbc1acbd', '5de0b487b07c402cc7d1a00f');
INSERT INTO `t_comment` (`id`, `create_time`, `content`, `topic_id`, `user_id`) VALUES ('5de0e1a45dd0a380dbc1accb', '2050-01-01 00:00:00', '真帅!', '5de0def05dd0a380dbc1acbb', '5de0b487b07c402cc7d1a00f');
INSERT INTO `t_comment` (`id`, `create_time`, `content`, `topic_id`, `user_id`) VALUES ('5de0e1b35dd0a380dbc1accc', '2050-01-01 00:00:00', '加油!', '5de0dd8f5dd0a380dbc1acb7', '5de0b487b07c402cc7d1a00f');
INSERT INTO `t_comment` (`id`, `create_time`, `content`, `topic_id`, `user_id`) VALUES ('5de0e20d5dd0a380dbc1accd', '2050-01-01 00:00:00', '大家快举报得了,居心何在', '5de0dfdf5dd0a380dbc1acbd', '5de0d74a5dd0a380dbc1aca7');
INSERT INTO `t_comment` (`id`, `create_time`, `content`, `topic_id`, `user_id`) VALUES ('5de0e21d5dd0a380dbc1accf', '2050-01-01 00:00:00', '赞!', '5de0def05dd0a380dbc1acbb', '5de0d74a5dd0a380dbc1aca7');
INSERT INTO `t_comment` (`id`, `create_time`, `content`, `topic_id`, `user_id`) VALUES ('5de0e22e5dd0a380dbc1acd0', '2050-01-01 00:00:00', '搞笑!', '5de0dd105dd0a380dbc1acb5', '5de0d74a5dd0a380dbc1aca7');
INSERT INTO `t_comment` (`id`, `create_time`, `content`, `topic_id`, `user_id`) VALUES ('5de0e23b5dd0a380dbc1acd1', '2050-01-01 00:00:00', '哈哈啊哈,悲催', '5de0dc555dd0a380dbc1acb3', '5de0d74a5dd0a380dbc1aca7');
INSERT INTO `t_comment` (`id`, `create_time`, `content`, `topic_id`, `user_id`) VALUES ('5de0e2695dd0a380dbc1acd2', '2050-01-01 00:00:00', '吃饭去啦!', '5de0dfdf5dd0a380dbc1acbd', '5de0d8745dd0a380dbc1aca9');
INSERT INTO `t_comment` (`id`, `create_time`, `content`, `topic_id`, `user_id`) VALUES ('5de0e2745dd0a380dbc1acd4', '2050-01-01 00:00:00', '666', '5de0def05dd0a380dbc1acbb', '5de0d8745dd0a380dbc1aca9');
INSERT INTO `t_comment` (`id`, `create_time`, `content`, `topic_id`, `user_id`) VALUES ('5de0e2855dd0a380dbc1acd5', '2050-01-01 00:00:00', '牛逼!', '5de0dd105dd0a380dbc1acb5', '5de0d8745dd0a380dbc1aca9');
INSERT INTO `t_comment` (`id`, `create_time`, `content`, `topic_id`, `user_id`) VALUES ('5de0e28e5dd0a380dbc1acd6', '2050-01-01 00:00:00', '666', '5de0dc555dd0a380dbc1acb3', '5de0d8745dd0a380dbc1aca9');
INSERT INTO `t_comment` (`id`, `create_time`, `content`, `topic_id`, `user_id`) VALUES ('ad759fb9-899c-42a3-a9fc-3437b3f6640c', '2021-04-30 19:18:05', '123', '5de0d65b5dd0a380dbc1aca5', '5de0d2696e78ad79c2e2c64a');
INSERT INTO `t_comment` (`id`, `create_time`, `content`, `topic_id`, `user_id`) VALUES ('ff6ce34b-2793-4289-9986-b2a3f99ef088', '2021-04-30 19:23:38', '456', '5de0d65b5dd0a380dbc1aca5', '5de0d2696e78ad79c2e2c64a');

2.获取评论——tornado查询接口

2.1查看前端需要的接口和传递的数据

发现,他们通过文章的id为标识去查看评论
在这里插入图片描述
在这里插入图片描述

2.2代码展示

接口

# forum/handler/CommentHandler.py
from forum.handler.BaseHandler import BaseHandler
from forum import manager
from forum.models import UserModel, TopicModel, CommentModel# 获取评论接口
class GetCommentHandler(BaseHandler):async def post(self):rs_data = {}id = self.get_body_argument('id')# 根据帖子ID获取所有的评论: 多表查询comments = await manager.execute(CommentModel.select().join(TopicModel).where(TopicModel.id==id))data = []# 遍历评论,转化成jsonfor c in comments:# 把评论转为jsontc = c.to_json()# 往转成的json对象种增加user属性tc['user'] = c.user.to_json()del tc['topic']data.append(tc)# 传递给前端rs_data['code'] = 200rs_data['msg'] = '获取评论成功'rs_data['comments'] = dataprint(rs_data['comments'])self.finish(rs_data)

挂载路由

# router.py
from forum.handler import BaseHandler,UserHandler,TopicHandler,CommentHandler
handlers = [('/api/topic/my/?', TopicHandler.GetMyTopicHandler),('/api/comment/get/tid/?',CommentHandler.GetCommentHandler)
]

3.发表评论——tornado增加接口

3.1查看前端需要的接口和传递的数据

在这里插入图片描述
在这里插入图片描述

3.2代码展示

接口

# forum/handler/CommentHandler.py
from uuid import uuid4from forum.handler.BaseHandler import BaseHandler
from forum import manager
from forum.models import TopicModel, CommentModel, UserModel
from forum.decorators import login_required_async
# 增加评论接口
class AddCommentHandler(BaseHandler):@login_required_asyncasync def post(self):# 前端传递来了两个数据,但是增加到评论表时,还得需要用户id,所以使用装饰器的方式获取评论用户的idcontent = self.get_body_argument('content')topic_id = self.get_body_argument('topic_id')# 获取帖子对象信息topic = await manager.get(TopicModel, id=topic_id)# 获取用户对象信息user = await manager.get(UserModel, id=self._user_id)# 创建一个Comment对象await manager.create(CommentModel, id=uuid4(), content=content, topic=topic, user=user)self.finish({'code':200,'msg':'评论成功!!!'})

挂载路由

# router.py
from forum.handler import BaseHandler,UserHandler,TopicHandler,CommentHandler
handlers = [('/',BaseHandler.IndexHandler),('/api/user/add/?', UserHandler.AddUserHandler),('/api/comment/add/?',CommentHandler.AddCommentHandler)
]

4.获取个人评论——tornado查询接口

4.1查看前端需要的接口和传递的数据

在这里插入图片描述

4.2代码展示

接口

# forum/handler/CommentHandler.py
from uuid import uuid4from forum.handler.BaseHandler import BaseHandler
from forum import manager
from forum.models import TopicModel, CommentModel, UserModel
from forum.decorators import login_required_async
# 查询自己的所有评论
class GetMyCommentHandler(BaseHandler):@login_required_asyncasync def post(self):# 获取评论# peewee_async多表查询comments = await manager.execute(CommentModel.select().join(UserModel).where(UserModel.id == self._user_id))# 建立一个列表,用来存储所有的评论数据<json类型>data = []# 遍历数据,将数据转化成json类型for c in comments:tc = c.to_json()tc['user'] = c.user.to_json()del tc['topic']data.append(tc)self.finish({'code':200,'msg':'获取个人评论成功!','comments':data})

创建路由

# router.py
from forum.handler import BaseHandler,UserHandler,TopicHandler,CommentHandler
handlers = [('/api/comment/add/?',CommentHandler.AddCommentHandler),('/api/comment/my/?',CommentHandler.GetMyCommentHandler)
]

这篇关于13.评论模块——peewee创建模型、tornado-peewee-async查询增加接口的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/468766

相关文章

CentOS7增加Swap空间的两种方法

《CentOS7增加Swap空间的两种方法》当服务器物理内存不足时,增加Swap空间可以作为虚拟内存使用,帮助系统处理内存压力,本文给大家介绍了CentOS7增加Swap空间的两种方法:创建新的Swa... 目录在Centos 7上增加Swap空间的方法方法一:创建新的Swap文件(推荐)方法二:调整Sww

Python logging模块使用示例详解

《Pythonlogging模块使用示例详解》Python的logging模块是一个灵活且强大的日志记录工具,广泛应用于应用程序的调试、运行监控和问题排查,下面给大家介绍Pythonlogging模... 目录一、为什么使用 logging 模块?二、核心组件三、日志级别四、基本使用步骤五、快速配置(bas

SpringBoot实现接口数据加解密的三种实战方案

《SpringBoot实现接口数据加解密的三种实战方案》在金融支付、用户隐私信息传输等场景中,接口数据若以明文传输,极易被中间人攻击窃取,SpringBoot提供了多种优雅的加解密实现方案,本文将从原... 目录一、为什么需要接口数据加解密?二、核心加解密算法选择1. 对称加密(AES)2. 非对称加密(R

MySQL中like模糊查询的优化方案

《MySQL中like模糊查询的优化方案》在MySQL中,like模糊查询是一种常用的查询方式,但在某些情况下可能会导致性能问题,本文将介绍八种优化MySQL中like模糊查询的方法,需要的朋友可以参... 目录1. 避免以通配符开头的查询2. 使用全文索引(Full-text Index)3. 使用前缀索

Java对接Dify API接口的完整流程

《Java对接DifyAPI接口的完整流程》Dify是一款AI应用开发平台,提供多种自然语言处理能力,通过调用Dify开放API,开发者可以快速集成智能对话、文本生成等功能到自己的Java应用中,本... 目录Java对接Dify API接口完整指南一、Dify API简介二、准备工作三、基础对接实现1.

基于Redis实现附近商铺查询功能

《基于Redis实现附近商铺查询功能》:本文主要介绍基于Redis实现-附近商铺查询功能,这个功能将使用到Redis中的GEO这种数据结构来实现,需要的朋友可以参考下... 目录基于Redis实现-附近查询1.GEO相关命令2.使用GEO来实现以下功能3.使用Java实现简China编程单的附近商铺查询4.Red

mysql递归查询语法WITH RECURSIVE的使用

《mysql递归查询语法WITHRECURSIVE的使用》本文主要介绍了mysql递归查询语法WITHRECURSIVE的使用,WITHRECURSIVE用于执行递归查询,特别适合处理层级结构或递归... 目录基本语法结构:关键部分解析:递归查询的工作流程:示例:员工与经理的层级关系解释:示例:树形结构的数

SQL常用操作精华之复制表、跨库查询、删除重复数据

《SQL常用操作精华之复制表、跨库查询、删除重复数据》:本文主要介绍SQL常用操作精华之复制表、跨库查询、删除重复数据,这些SQL操作涵盖了数据库开发中最常用的技术点,包括表操作、数据查询、数据管... 目录SQL常用操作精华总结表结构与数据操作高级查询技巧SQL常用操作精华总结表结构与数据操作复制表结

Java 如何创建和使用ExecutorService

《Java如何创建和使用ExecutorService》ExecutorService是Java中用来管理和执行多线程任务的一种高级工具,可以有效地管理线程的生命周期和任务的执行过程,特别是在需要处... 目录一、什么是ExecutorService?二、ExecutorService的核心功能三、如何创建

MySQL 复合查询案例详解

《MySQL复合查询案例详解》:本文主要介绍MySQL复合查询案例详解,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录基本查询回顾多表笛卡尔积子查询与where子查询多行子查询多列子查询子查询与from总结合并查询(不太重要)union基本查询回顾查询