0

楷模

class post(models.Model):
    post_content = models.CharField(u'Treść:', max_length=65000)
    post_thread = models.ForeignKey(thread)
    post_user = models.ForeignKey(user)
    post_date = models.DateTimeField()
    objects = post_manager()

class thread(models.Model):
    thread_title = models.CharField(u'Nazwa tematu:', max_length=100)
    thread_content = models.CharField(u'Treść:', max_length=65000)
    thread_forum = models.ForeignKey(forum, null=True)
    thread_subforum  = models.ForeignKey(subforum, null=True)
    thread_tag = models.CharField(u'Tagi', null=True,  max_length=100)
    thread_user  = models.ForeignKey(user)
    thread_date = models.DateTimeField()
    thread_write = models.BooleanField()
    thread_sticky = models.BooleanField()
    objects = thread_manager()

意见

thread_list = thread.objects.select_related().filter(thread_forum_id = current_obj.id).order_by('-thread_date')
            for thread in thread_list:
                count = post.objects.select_related().filter(post_thread_id = thread.id).count()   
                thread.post = count

thread_list 没有帖子编号帖子有 ForeignKey 到线程

我能做些什么来达到这样的效果?我想将数字帖子添加到 thread_list

4

1 回答 1

0

你的方法很好。但是有一个冲突:

thread_list = thread.objects.select_related().filter(thread_forum_id = current_obj.id).order_by('-thread_date')
            for thread in thread_list:
                count = post.objects.select_related().filter(post_thread_id = thread.id).count()   
                thread.post = count

你的班级有名字线程,你的循环有班级的名字。对于thread_list中的线程:

只需这样做:

for threads in thread_list:
                    count = post.objects.select_related().filter(post_thread_id = threads.id).count()   
                    threads.post = count

或类似的东西。只需更改循环 - 我在单词末尾添加“s”

于 2013-10-29T08:33:15.357 回答