1👍
✅
You are working with a ListView
[Django-doc], not a DetailView
[Django-doc], and a ListView
indeed has no .get_object(…)
method [Django-doc]. You furthermore should specify a model = …
or queryset = …
to specify with what queryset the DetailView
is dealing.
You thus should inherit from the DetailView
. You can also work with self.object
to prevent making an extra query:
from django.views.generic import DetailView
class CategoryDetailView(LoginRequiredMixin, DetailView):
template_name = 'clients/category/category_detail.html'
context_object_name = 'category'
model = Category
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
clients = self.object.client_set.all()
context['clients'] = clients
return context
There is als no reason to add this to the context, in the template, you can simply work with:
{% for client in category.client_set.all %}
{{ client }}
{% endfor %}
Source:stackexchange.com