How to get all children (ForeignKey based) from parent objects in a django template (e.g all comments for a blog post) ?

Active May 02, 2022    /    Viewed 366    /    Comments 0    /    Edit

Example of how to get all children from parent objects in a django template: file

Let's consider the following case: a blog website in which a post can be created and commented as well:

class Blog_Post(models.Model):
        content = models.TextField()
        date_created = models.DateTimeField(

class Blog_Post_Comment(models.Model):
        blog_post = models.ForeignKey(Blog_Post, null=True, blank=True, default = None,on_delete=models.DO_NOTHING)
        content = models.TextField()
        date_created = models.DateTimeField(

So a single post can have multiple comments.

To display all posts in a django template (called for example blog.html), we can create a simple Django view:

def blog_view(request):
        blog_post_obj_list = Blog_Post.objects.all()
        context = {'blog_post_obj_list':blog_post_obj_list}
        return render(request, "myapp/blog.html", context )

Get all children in a django template

To iterate over all posts and show its content in the django template blog.html, a solution is to do

{% for blog_post_obj in blog_post_obj_list %}

    {{ blog_post_obj.content }}

{% endfor %}

Now to also get all comments for a single post, a solution is to do here (see Many-to-one relationships:


(just add the suffix _set.all to the child name: parent_obj.child_name_set.all )

{% for blog_post_obj in blog_post_obj_list %}

    {{ blog_post_obj.content }}

    {% for blog_post_comment_obj in blog_post_obj.blog_post_comment_set.all %}
        {{ blog_post_obj.content }}
    {% endfor %}

{% endfor %}

Get the number of comments:

Note 1: it is possible to get the number of comments:

{{ blog_post_obj.blog_post_comment_set.count }}

Sort comments by

Note 2: Comments can also be sorted by a given column, for example date_created (see dictsort)

{% for blog_post_comment_obj in blog_post_obj.blog_post_comment_set.all|dictsort:"date_created" %}


Card image cap

Hi, I am Ben.

I have developed this web site from scratch with Django to share with everyone my notes. If you have any ideas or suggestions to improve the site, let me know ! (you can contact me using the form in the welcome page). Thanks!

Did you find this content useful ?, If so, please consider donating a tip to the author(s). is visited by millions of people each year and it will help us to maintain our servers and create new contents.