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

相关文章

从入门到精通MySQL联合查询

《从入门到精通MySQL联合查询》:本文主要介绍从入门到精通MySQL联合查询,本文通过实例代码给大家介绍的非常详细,需要的朋友可以参考下... 目录摘要1. 多表联合查询时mysql内部原理2. 内连接3. 外连接4. 自连接5. 子查询6. 合并查询7. 插入查询结果摘要前面我们学习了数据库设计时要满

MySQL查询JSON数组字段包含特定字符串的方法

《MySQL查询JSON数组字段包含特定字符串的方法》在MySQL数据库中,当某个字段存储的是JSON数组,需要查询数组中包含特定字符串的记录时传统的LIKE语句无法直接使用,下面小编就为大家介绍两种... 目录问题背景解决方案对比1. 精确匹配方案(推荐)2. 模糊匹配方案参数化查询示例使用场景建议性能优

mysql表操作与查询功能详解

《mysql表操作与查询功能详解》本文系统讲解MySQL表操作与查询,涵盖创建、修改、复制表语法,基本查询结构及WHERE、GROUPBY等子句,本文结合实例代码给大家介绍的非常详细,感兴趣的朋友跟随... 目录01.表的操作1.1表操作概览1.2创建表1.3修改表1.4复制表02.基本查询操作2.1 SE

Python中re模块结合正则表达式的实际应用案例

《Python中re模块结合正则表达式的实际应用案例》Python中的re模块是用于处理正则表达式的强大工具,正则表达式是一种用来匹配字符串的模式,它可以在文本中搜索和匹配特定的字符串模式,这篇文章主... 目录前言re模块常用函数一、查看文本中是否包含 A 或 B 字符串二、替换多个关键词为统一格式三、提

MySQL数据库的内嵌函数和联合查询实例代码

《MySQL数据库的内嵌函数和联合查询实例代码》联合查询是一种将多个查询结果组合在一起的方法,通常使用UNION、UNIONALL、INTERSECT和EXCEPT关键字,下面:本文主要介绍MyS... 目录一.数据库的内嵌函数1.1聚合函数COUNT([DISTINCT] expr)SUM([DISTIN

python如何创建等差数列

《python如何创建等差数列》:本文主要介绍python如何创建等差数列的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录python创建等差数列例题运行代码回车输出结果总结python创建等差数列import numpy as np x=int(in

XML重复查询一条Sql语句的解决方法

《XML重复查询一条Sql语句的解决方法》文章分析了XML重复查询与日志失效问题,指出因DTO缺少@Data注解导致日志无法格式化、空指针风险及参数穿透,进而引发性能灾难,解决方案为在Controll... 目录一、核心问题:从SQL重复执行到日志失效二、根因剖析:DTO断裂引发的级联故障三、解决方案:修复

怎么用idea创建一个SpringBoot项目

《怎么用idea创建一个SpringBoot项目》本文介绍了在IDEA中创建SpringBoot项目的步骤,包括环境准备(JDK1.8+、Maven3.2.5+)、使用SpringInitializr... 目录如何在idea中创建一个SpringBoot项目环境准备1.1打开IDEA,点击New新建一个项

如何使用Maven创建web目录结构

《如何使用Maven创建web目录结构》:本文主要介绍如何使用Maven创建web目录结构的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录创建web工程第一步第二步第三步第四步第五步第六步第七步总结创建web工程第一步js通过Maven骨架创pytho

mysql查询使用_rowid虚拟列的示例

《mysql查询使用_rowid虚拟列的示例》MySQL中,_rowid是InnoDB虚拟列,用于无主键表的行ID查询,若存在主键或唯一列,则指向其,否则使用隐藏ID(不稳定),推荐使用ROW_NUM... 目录1. 基本查询(适用于没有主键的表)2. 检查表是否支持 _rowid3. 注意事项4. 最佳实