本文主要是介绍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查询增加接口的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!