【发布时间】:2017-05-20 01:59:48
【问题描述】:
我正在尝试创建一个评论系统作为爱好项目的一部分,但我无法弄清楚一旦从数据库中获取评论对象后如何对它们进行递归排序。我正在使用具有以下数据模型的关系数据库:
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>, ...]} ...
任何评论对象都可以有无限数量的子对象。几乎就像 reddit 和其他类似社交媒体网站上使用的评论系统一样。 对于渲染,我使用的是烧瓶和 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 %}
在此之前我如何对数据进行排序是我没有弄清楚的。
【问题讨论】:
标签: python algorithm sqlite recursion pymysql