gpt4 book ai didi

python - 递归地将pymysql Comment对象转换为树

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:15:37 25 4
gpt4 key购买 nike

我正在尝试创建一个评论系统作为业余爱好项目的一部分,但我不知道如何在从数据库中获取 Comment 对象后对其进行递归排序。我正在使用具有以下数据模型的关系数据库:

class Comment(Base):
__tablename__ = 'comments'
id = Column(Integer, primary_key=True)
comment = Column(String(), nullable=False)
user_id = Column(Integer, ForeignKey('users.id'), nullable=False)
post_id = Column(Integer, ForeignKey('posts.id'), nullable=False)
parent_id = Column(Integer, ForeignKey('comments.id'), nullable=False)

从数据库中获取数据后,我需要在树中对这些对象进行排序。例如,示例输入可以是:

comments = [
<models.Comment object at 0x104d80358>,
<models.Comment object at 0x104d803c8>,
<models.Comment object at 0x104d80470>,
<models.Comment object at 0x104d80518>,
<models.Comment object at 0x104d805c0>,
<models.Comment object at 0x104d80668>
]

预期结果可能是:

comment_dict = {1: {'comment':<Comment.object>, 'children':[]},
{2: {'comment':<Comment.object>, 'children':[<Comment.object>, ...]},
{3: {'comment':<Comment.object>, 'children':[]},
{4: {'comment':<Comment.object>, 'children':[<Comment.object>, ...]} ...

任何评论对象都可以有无限数量的 child 。几乎就像 reddit 和其他类似社交媒体网站上使用的评论系统。对于渲染,我使用的是 flask 和 Jinja,并且可能会做一些我在文档中找到的类似的事情:

<ul class="sitemap">
{%- for item in sitemap recursive %}
<li><a href="{{ item.href|e }}">{{ item.title }}</a>
{%- if item.children -%}
<ul class="submenu">{{ loop(item.children) }}</ul>
{%- endif %}</li>
{%- endfor %}

我不知道在执行此操作之前如何对数据进行排序。

最佳答案

非常简单的方法是这样的:

def comments_to_dict(comments):
result = {}
for comment in comments:
result[comment.id] = {
'comment': comment,
'children': []
}
for comment in comments:
result[comment.parent_id]['children'].append(comment)
return result

所以首先你用 children 填充根元素为空,然后在第二遍中填充子元素。这可以通过仅对 comments 进行一次传递来进一步改进:

def comments_to_dict(comments):
result = {}
for comment in comments:
if comment.id in result:
result[comment.id]['comment'] = comment
else:
result[comment.id] = {
'comment': comment,
'children': []
}

if comment.parent_id in result:
result[comment.parent_id]['children'].append(comment)
else:
result[comment.parent_id] = {
'children': [comment]
}
return result

此处的解决方案与您向我们展示的预期输出相匹配。


如果你想要一棵真正的树,那么试试这个

def comments_to_dict(comments):
index = {}
for comment in comments:
index[comment.id] = {
'comment': comment,
'children': []
}
for obj in index.itervalues():
pid = obj['comment'].parent_id
index[pid]['children'].append(obj)
return index

关于python - 递归地将pymysql Comment对象转换为树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41486506/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com