事实证明,解决方案比预期的要简单得多。我试图使用 get_queryset 当它与ListView一起使用时,与通用DetailView一起使用。
get_queryset
我重写了模型使用 get_context_data 功能而不是。
get_context_data
class HostView(generic.DetailView): model = Host template_name = 'report_viewer/host.html' def get_context_data(self, **kwargs): """Return all related findings""" context = super(HostView, self).get_context_data(**kwargs) context['host'] = get_object_or_404(Host, name=self.kwargs["pk"]) context['findings'] = context['host'].get_findings() return context
然后,这允许我访问模板中的相关主机模型及其所有相关的查找模型。