如何使用 DetailView 显示内嵌内容?

发布于 2024-11-19 17:24:48 字数 786 浏览 3 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最终幸福 2024-11-26 17:24:48

不存在内联模型这样的东西。有内联表单,它们是与父模型具有外键关系的模型的表单 - 但您似乎没有在谈论表单。

无论如何,不​​需要在代码中执行任何操作。可以直接在模板中引用相关模型:

{% for day in object.day_set.all %}
    {{ day.whatever }}
{% endfor %}

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:

{% for day in object.day_set.all %}
    {{ day.whatever }}
{% endfor %}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文