我正在构建一个网站的论坛应用程序,并在Posts表中有一个“quote_id”列,以便在post_id中添加一个引用某个帖子来回复的帖子至。与其他所有帖子一样,这是Posts表的一部分。但是,当我检索与所选主题相对应的主题相匹配的所有记录时,会有一些引用其他帖子的记录。我似乎无法找到在Django ORM中执行此操作的方法,以检索引用的帖子信息以及引用该帖子的帖子(在引用该帖子的帖子中显示)。到目前为止我所得到的是:Django:根据列vsvalue从数据记录中获取同一表的信息
def show_topic_posts(request,forum_id,topic_id):
posts = Post.objects.filter(topic_id=topic_id)
topic = Topic.objects.get(topic_id=topic_id)
context = RequestContext(request,{'posts':posts,'topic':topic})
return render_to_response('forums/topics_posts.html',context_instance=context)
class Post(models.Model):
post_id = models.AutoField(primary_key=True)
forum_id = models.ForeignKey(Forum)
topic_id = models.ForeignKey(Topic)
post_date_time = models.DateTimeField(auto_now=True)
post_user_id = models.ForeignKey(User)
post_quote_id = models.IntegerField(null=True,blank=True)
post_body = models.TextField()
post_likes = models.IntegerField(default=0)
post_is_flagged = models.BooleanField(default=False)
def __unicode__(self):
return '%i' % self.topic_id
def get_absolute_url(self):
return '/forums/%s/%s/%s/' % (str(self.forum_id.pk), str(self.topic_id.pk),self.topic_id)
在世界上我怎么能做到这一点?我搜索了Google和Django Book和Django网站,试图找到一种方法来做到这一点。谢谢!
因此,将post_quote_id更改为ManyToMany('self')?我做到了,然后在每篇文章上方将我的模板更改为此。这是你的意思吗? {%为岗位员额%} { 如果%%post.post_quote}
不知道我是否完全理解你做了什么或想做什么,但看到我的答案补充。要获得帖子的相关帖子/报价,您必须循环查看帖子。 – arie 2011-04-17 20:33:38
那么你在你的修改后的帖子中所做的就是我所需要做的。谢谢你的帮助。我只是一个有经验的中级程序员,我无法在任何地方找到答案。非常感谢! – vol4life27 2011-04-17 20:55:55