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

  • SEARCH
  • Home
  • 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 8450237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:55:02+00:00 2026-06-10T10:55:02+00:00

I have a Django project that has two models: Group and Person. Groups can

  • 0

I have a Django project that has two models: Group and Person. Groups can contain either Person objects or other Group objects. Groups cannot form a cycle (i.e. Group A containing Group B containing Group A), which results in a tree structure where Person objects are leaves.

My question is – how can I count all the contained Group objects and Person objects within a high level Group (like the root Group) with as few SQL queries as possible?

A naive approach with O(N) (where N is # of subgroups) SQL queries would be:

def Group(models.Model):
    name = models.CharField(max_length=150)
    parent_group = models.ForeignKey('self', related_name=child_groups, null=True, blank=True)

    # returns tuple (# of subgroups, # of person objects)
    def count_objects(self):
        count = (self.child_groups.count(), self.people.count())
        for child_group in self.child_groups.all():
            # this adds tuples together ( e.g: (1,2) and (1,2) make (2,4) )
            tuple(map(operator.add, count, child_group.count_objects()))

def Person(models.Model):
    user = models.ForeignKey(User)
    picture = models.ImageSpecField(...)
    group = models.ForeignKey('Group', related_name="people")

Is there a way to improve this or should I just store these values within the Group object?

  • 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-06-10T10:55:03+00:00Added an answer on June 10, 2026 at 10:55 am

    So this is an existing problem that many others have tackled. If you’re using Django, check this out:
    http://django-mptt.github.com/django-mptt/index.html

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

Sidebar

Related Questions

I have an existing Django project that has several models using concrete inheritance of
I have Django project on Dreamhost server which has several views that returns Json
I have a project model that has a FK to a group model. Group
I have a Django project that, on one page, has multiple forms (in different
So in my Django project, I've created a Notification model that has two datetime
I have a Django project which has two apps (one created as debug test).
I have a Django project, that has a Address model. This is used in
I have a django project which has two apps, one is AppA and AppB
I have a django project that I have been working on as a solo
I have a django project that runs on a Linux server, and I've been

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.