Reputation: 9527
In my DetailView, I want to get the object as per the kwargs in my url, plus also retrieve all the related (foreignkey) values of it.
I use:
queryset = Category.objects.select_related()
in the views, however, trying to access the related data using
{% for i in category.all %}
However, nothing shows in template when rendered.
I tried this in template
{% for i in category.toolkit_set %}
and it said the related object isn't iterable
I have in a jiffy, this my models.py:
class Category(models.Model):
....
title = models.CharField(max_length=250)
....
class Toolkit(models.Model);
....
category = models.ForeignKey('Category')
I want to get the object of Category per the slug, plus retrieve all the associated related data in one db hit.
Upvotes: 4
Views: 1361
Reputation: 599788
Firstly, you have to use the same variable name to access the Category object as you have elsewhere in the template. Usually in a DetailView, unless you've explicitly changed it, the object name is just object
.
Secondly, you have the two separate parts of accessing the related objects - _set
and all
- but you don't seem to have put them together.
So, it should be:
{% for toolkit in object.toolkit_set.all %}
Upvotes: 5