我无法发布图片,因为我是新手,因此这里是what I want的链接。如何使用我的实际django模型优化数据库查询?
所以,我有模型在左边,我想要在右边的视图。 。
截至目前,我在每一个线程我感兴趣的模板代码遍历:
{% for thread in threadlist %}
{% for post in thread.postlist %}
...
型号代码:
class Thread (models.Model):
...
def postlist(self):
posts = list(self.post_set.all())
return [posts.pop(0)] + posts[-2:]
必须有办法用更少的查询,要做到这一点通过加入专栏或其他东西。我对Python/Django相当陌生,我不知道该怎么做。
我正在为每个线程进行查询,即每页10个查询。 我只想要1个循环,而不是有2个嵌套的循环。 “QuerySets懒惰”意味着它们只在必要时执行,并且在这里每个都是。我的实际解决方案不是最佳的。 – knarf 2009-08-24 12:57:53