我有两种模式:
Tutorial
--> consist of published manager which returns queryset when is_published=True
Category
在模板中,我传递的是Category对象。
{% for category in categories %}
{% for tutorial in category.tutorial_set.all %}
{{ tutorial.title }}
{% endfor %}
{% endfor %}
我想从已发布的经理那里得到:Tutorials.published.all()
如何实现这一目标?
答案 0 :(得分:1)
嗯,我猜你可以做点像
class TutuorialManager(models.Manager):
def published(self):
return self.filter(is_published = True)
然后在视图中你可以做类似的事情。
{% for category in categories %}
{% for tutorial in category.tutorial_set.published.all %}
{{ tutorial.title }}
{% endfor %}
{% endfor %}