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 6816431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:55:30+00:00 2026-05-26T20:55:30+00:00

I want to present general statistics about the instances of the class ModelClass on

  • 0

I want to present general statistics about the instances of the class “ModelClass” on a web page for my users. Lets say there are some thousands ModelClass-objects, and that there are a lot of statistics that I need to calculate. I have figured out that I can do this with model managers, and here is a (very) simplified example:

class ModelClassCustomManager(models.Manager):
    def get_query_set(self):
        return super(ModelClassCustomManager, self).get_query_set().filter(is_complete = True)


class ModelClass(models.Model):
    is_complete = models.BooleanField(default = False)
    ...
    objects = models.Manager()
    complete_objects = ModelClassCustomManager()

My concern is that this drains a lot of resources if this is calculated when users view the page. I would therefore like to calculate it only when I change or create new ModelClass objects since this is the only time the statistics really change. I guess this can be done by overriding the ModelClass save()-method.

What is the best way to save these results? Should I create another django-model for holding the calculated statistics, or is there another way of storing this information?

EDIT: Thanks to pastylegs for a good answer. Doing it this way, however causes some minor bugs, and I figured I’ll explain them here, in case anyone else runs into this issue.

First of all, I incorrectly put the imports in the ModelClass at the top, so I got a circular dependency, which gave some weird results. To save yourself some frustration, put them where pastylegs did. Secondly, in order to overwrite the previous calculations of statistics (and not create a new one every time), just replace

if sender is ModelClass and instance is not Null:
    count = ModelClass.objects.all().count()
    stat = Stat(name='some_name', value=count).save()

with this:

if sender is ModelClass:
    count = Match.objects.all().count()
    try:
        stat = Stat.objects.get(key="Total")
        stat.update(key="Total", value=count) #Update statistic
    except: 
        Stat(key="Total", value=count).save() #Create new
  • 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-26T20:55:31+00:00Added an answer on May 26, 2026 at 8:55 pm

    I’d create an app called statistics (or similar) that has a simple model to hold key,value pairs:

    class Stat(models.Model):
        key = models.CharField(..)
        value = models.CharField(...)
    

    then use a signal on your own model to run a function every time a user saves/updates a instance of your model. In models.py:

    class ModelClass(models.Model):
        ...
    
    from django.db.models.signals import post_save
    from myapp.signal import updates_stats
    post_save.connect(updates_stats, sender=ModelClass)
    

    and then create the receiver function to calculate what ever statistics you need in signals.py

    from myapp.models import ModelClass
    from stats.models import Stat
    def update_stats(sender, instance, signal, *args, **kwargs):
        if sender is ModelClass and instance is not Null:
            count = ModelClass.objects.all().count()
            stat = Stat(name='some_name', value=count).save()
    

    This is a very simple and basic approach and only outlines how you can make use of signals to perform calculations but it is a good start. Ideally you should try to calculate these values outside of the request/response cycle (especially if they are big caculations) as it will hang your server and potentially time out your requests, so you should consider using a queuing/task system like Celery to perform the calculation in the background

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

Sidebar

Related Questions

I have general question about designated initializer. I have a some class and from
I create some KML files in my project and I want to present on
I have two screens on my app that I want to present to users,
I want to show all users present in a database. I want to place
tl;dr general question about handling database data and design: Is it ever acceptable/are there
I want to present some images in my GWT application. These images are stored
I want to present an iPad game which is only playable in portrait mode
I want to present a modalViewController with a small frame and at the center
I want to present an alert window with no button on it, that means
In my application I want to present a list of choices for a user

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.