简体   繁体   English

Django-使用树构建注释系统

[英]Django - Using trees to build a comment system

A few days back I was messing around with Django, trying to get a feel for how stuff works, when I decided to try and build a simple forum, one that resembled a forum that I frequented (but is now closed down). 几天前,当我决定尝试建立一个简单的论坛(类似于我经常光顾的一个论坛,但现在已关闭)时,我正与Django混为一谈,以尝试了解东西的工作原理。 The idea was that each of the comments would be parent to any number of comments, like so: 想法是每个评论都可以是任意数目评论的父级,例如:

 comment <--top
   comment <-- comment "A"
   comment <-- comment "B"
   comment <-- comment "C"
     comment <--C-1, reply to comment "C"
       comment <-- C-1-1, reply to comment "C-1"
         comment 
           comment 
             comment
         comment <-- C-1-1-1 reply to C-1-1
         comment 
         comment
           comment
             comment
     comment
     comment
       comment
         comment
           comment
             comment
             comment
             comment

The idea here is that replies to a comment would stuffed one level beneath it, and each comment, with the exception of the very first comment, has a parent. 这里的想法是,对评论的回复将在其下方填充一个级别,并且每个评论(除了第一个评论之外)都有一个父级。 The thing is, although I get the idea behind implementing tree traversals, none of the books/articles I've read on the subject take Django into account (or the MVC pattern for that matter), so my question is how would I go about implementing this system in Django? 问题是,尽管我有了实现树遍历的想法,但是我所读过的书/文章都没有考虑Django(或者说是MVC模式),所以我的问题是我该怎么做在Django中实现此系统? (here's the model code i've got for reference :-/) (这是我可以参考的模型代码:-/)

class Comment(models.Model): 
 Parent = models.OneToOneField('self', null=True)
 Children = models.ForeignKey('self', null=True)

 Author = models.ForeignKey(User)
        Author_IP = models.IPAddressField()
 Created_On = models.DateTimeField(auto_now_add=True)
 Modified_On = models.DateTimeField(auto_now=True)
 Body = models.TextField()

Have a look at django-threadedcomments . 看看django-threadedcomments It's purpose is more fit to be used as comments on a blog than a full featured forum, but if it doesn't fit your case, you can at least look at the source code and learn a couple things from it. 目的是更适合用作博客评论,而不是功能齐全的论坛,但如果它不适合您的情况,则您至少可以查看源代码并从中学习一些内容。

As far as tree-based structures go, there are three projects I'm aware of for Django's ORM: django-mptt (this one has the biggest "market share" with 3rd party django apps AFAIK), django-treebeard , and easytree (which is based on treebeard). 就基于树的结构而言,我知道针对Django ORM的三个项目: django-mptt (这是第三方django应用AFAIK的最大“市场份额”), django-treebeardeasytree (基于胡须)。 Easytree comes with a nice admin interface, but the other two projects have at least patches in their issue trackers to add an admin interface (not sure if they integrated those patches already). Easytree带有一个不错的管理界面,但是其他两个项目的问题跟踪器中至少有补丁程序可以添加管理界面(不确定是否已经集成了这些补丁程序)。

I would only define the parent and give it a related name 我只会定义父对象并给它一个相关的名称

class Comment(models.Model):
  parent=models.ForeignKey('self', related_name="children", null=True, blank=True)
  #other fields 

Then you could get its children 那你就可以得到它的孩子

comment=Comment.objects.get(id=1)
children=comment.children.all()

for child in children:
  morechildren=child.children.all()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM