如何使用 DetailView 显示内嵌内容?
我有一个 Project
模型。 该模型具有内联的Days
。
如何使用 DetailView 显示它们?
我的views.py看起来像这样:
class ProjectDetailView(DetailView):
queryset = Project.objects.all()
slug_field = 'slug'
template_name = 'projects/detail_project.html'
How do I pull through the Day inlines with this?
我试过了:
def get_context_data(self, **kwargs):
context = super(ProjectDetailView, self).get_context_data(**kwargs)
project = Project.objects.filter(slug=self.slug_field)
context['days'] = Day.objects.filter(project=project)
return context
但这不起作用。另外,我使用通用视图但随后执行 get_object_or_404
来提取 Days
似乎毫无意义。
我该如何正确执行此操作?
I have a Project
model.
This model has Days
which are inlines.
How do I display them using a DetailView?
My views.py looks like this:
class ProjectDetailView(DetailView):
queryset = Project.objects.all()
slug_field = 'slug'
template_name = 'projects/detail_project.html'
How do I pull through the Day inlines with this?
I've tried:
def get_context_data(self, **kwargs):
context = super(ProjectDetailView, self).get_context_data(**kwargs)
project = Project.objects.filter(slug=self.slug_field)
context['days'] = Day.objects.filter(project=project)
return context
But this doesn't work. Also it seems pointless that I'm using a Generic view but then doing a get_object_or_404
anyway to pull the Days
out.
How do I do this properly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不存在内联模型这样的东西。有内联表单,它们是与父模型具有外键关系的模型的表单 - 但您似乎没有在谈论表单。
无论如何,不需要在代码中执行任何操作。可以直接在模板中引用相关模型:
There's no such thing as an inline model. There are inline forms, which are forms for a model which has a ForeignKey relationship with a parent model - but you don't seem to be talking about forms.
In any case, there's no need to do anything in code. You can refer to the related models directly in the template: