我使用哪个视图子类来获取具有分页但仍然具有反向匹配的线程和论坛?

Which view subclass do I use to get the threads and forum with pagination but still have reverse matching?

models.py

class Forum(models.Model):
  ...

class Thread(models.Model):
  ...
  forum = models.ForeignKey(Forum, related_name="threads")

我想要一个包含特定论坛主题的页面:

我试过了

views.py

class ForumView(ListView):
  template_name = ...
  model = Thread

但我不确定如何访问包含所有这些主题的特定论坛——特别是如果我想添加一个新主题——我需要访问该论坛的 pk,以便我可以将其用于反向匹配。

覆盖 ListView 上的 get_queryset() 以过滤列表,例如。

# urls.py
url(r'^forum/(?P<pk>\d+)/$', views.ForumView.as_view(), name='forum') 

# views.py
class ForumView(ListView):
    template_name = ...
    model = Thread

    def get_queryset(self):
        return self.model.objects.filter(forum=self.kwargs['pk'])
        # you could do the same thing using the related name but you
        # would then need to fetch the Forum instance first, eg:
        # forum = models.Forum.objects.get(pk=self.kwargs['pk'])
        # return forum.threads.all()