Django将新项目添加到列表中

时间:2013-10-27 19:32:13

标签: django

模型

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没有帖子号码 post有ForeignKey来线程

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

1 个答案:

答案 0 :(得分: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'

相关问题