【发布时间】:2013-11-24 06:11:07
【问题描述】:
我有 1 多个相关模型。
class Foo(models.Model):
foo_data = models.IntegerField()
class Bar(models.Model):
foo = models.ForeignKey(Foo, related_name="bars")
bar_data = models.IntegerField()
paginator = Paginator(Foo.objects.filter(id=foo_id), 10)
page = 1
foos = paginator.page(page)
我需要在 django 模板中显示每个 foo 对象的 bar 数据。
首选的方法是什么?如何做?
- 从模板执行数据库查询(inclusion_tag 之类的?)
- 在视图中执行必要的数据库查询并以某种方式将其传递给模板
【问题讨论】:
-
在视图中进行分页比在模板中更好。
-
你的意思是你也需要分页栏吗?
-
我需要在模板中显示 foo.bars。
标签: python django templates pagination