Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 378863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T14:50:09+00:00 2026-05-12T14:50:09+00:00

In a Django application, I want to display a multi-level (but fixed-depth) tree of

  • 0

In a Django application, I want to display a multi-level (but fixed-depth) tree of model objects in table form. The HTML would look something like this:

<tr><td rowspan="2">group 1</td><td>item 1.1</td></tr>
<tr><td>item 1.2</td></tr>
<tr><td rowspan="3">group 2</td><td>item 2.1</td></tr>
<tr><td>item 2.2</td></tr>
<tr><td>item 2.3</td></tr>

The problem is filling in the rowspan. It’s easy enough for two levels: you just use group.item_set.count. But let’s say we have another level after items (eg subitem 1.1.1 etc.): then the rowspan of the “group 1” cell needs to be the count of all items plus the sum of all subitems within all items. It’s easy enough to calculate using an aggregate, but I can’t use aggregates in Django’s template language.
That leaves a few options:

  • Add a count_all_subitems method to the Group model class. However, it seems wrong to put extra code in the model just because the view layer needs it.
  • Generate a dictionary or list of subitem counts in the view function and pass it as extra contex; however, Django’s template language does not allow the key/index in a dictionary/list lookup to be a variable (eg, subitem_counts.group does subitem_counts['group'], and there’s no way to make it do subitem_counts[group] AFAIK), so this requires a custom filter.
  • Write a custom filter or tag that calculates and outputs the subitem count (or the entire rowspan value) directly.

I know Django is designed to avoid having much logic in the templates, and instead recommends putting it in the view function, but it seems that I still need an extra piece (a custom template filter or tag) to actually use the results in a template. What’s the preferred approach?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T14:50:09+00:00Added an answer on May 12, 2026 at 2:50 pm

    In the view you can use the aggregation “annotate” method to add a subitem_count value to the Group model queryset that is passed to the view without modifying the actual model definition.

    A basic example is included in the answers to this SO question.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Django and Python 2.6, and I want to grow my application using
When starting a django application using python manage.py shell , I get an InteractiveConsole
In my django application I am using a template to construct email body, one
I am building a relatively simple Django application and apart from the main page
I'm working on my first Django application. In short, what it needs to do
I am working on an django application that will return what historically was a
I have set up a Django application that uses images. I think I have
I am writing a fairly simple Django application where users can enter string queries.
I'm writing a basic Django application. For testing / development purposes I'm trying to
I'm struggling with the design of a django application. Given the following models: class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.