如何在Django中编写一个DetailView,它有其他相关的查询?
以下是我的例子:
class DistributionDetailView(DetailView):
model = Distribution
template_name = 'projects/view_distribution.html'
def get_context_data(self, **kwargs):
context = super(DistributionDetailView,
self).get_context_data(**kwargs)
context['weekly_attendance'] = WeeklyAttendance.objects.filter(
distibution_name=Distribution)
context['sales_data'] = SalesData.objects.filter(
distibution_name=Distribution)
context['theatrical_release'] = TheatricalRelease.objects.filter(
distibution_name=Distribution)
return context
答案 0 :(得分:0)
Distribution
是模型类,您无法在filter()
中使用它。在get_context_data
方法中,您可以使用self.object
访问当前对象,例如:
context['weekly_attendance'] = WeeklyAttendance.objects.filter(
distibution_name=self.object)
答案 1 :(得分:0)
您可以在大多数通用视图中使用self.object
访问该对象:
class DistributionDetailView(DetailView):
model = Distribution
template_name = 'projects/view_distribution.html'
def get_context_data(self, **kwargs):
context = super(DistributionDetailView,
self).get_context_data(**kwargs)
context['weekly_attendance'] = WeeklyAttendance.objects.filter(
distibution=self.object)
context['sales_data'] = SalesData.objects.filter(
distibution=self.object)
context['theatrical_release'] = TheatricalRelease.objects.filter(
distibution=self.object)
return context