Search code examples
djangodjango-generic-viewsdjango-class-based-views

How do i display inlines with DetailView?


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?


Solution

  • 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 %}