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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:21:49+00:00 2026-06-06T19:21:49+00:00

class A(models.Model): def calculate(self): # calculate some field based on all self.b_set instances class

  • 0
class A(models.Model):
    def calculate(self):
        # calculate some field based on all self.b_set instances

class B(models.Model):
    a = models.ForeignKey(A)

I’d like saving an instance of B to cause a function to fire on A, but efficiently.

A naive implementation would be to hook up to the post_save signal emitted by B instances then run b.a.calculate(), but it would call a.calculate() N times.

Does anybody have any suggestions for how to implement this more efficiently?

I’m hitting a roadblock because I’m not sure how I would determine when the last B instance was successfully updated after which I’d want the A instance run its potentially expensive calculate() function.

My best idea so far might be a “changed” flag on A and have a cron job pick up which A models need to be updated, but I wish it could be more accurate.

  • 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-06T19:21:50+00:00Added an answer on June 6, 2026 at 7:21 pm

    If you have a formula x = a+b+c then you would always want to recalculate x after any of the arguments a,b or c changes. In your model above, if the calculated field value in model A depends on the relationships in model B then you would always want to recalculate after one of B instances change. However that does not necessary means the post_save will run N times. It will only run once every time one of B instances get saved.

    If B has many fields that don not affect the expensive A.calculate() function, then adding a temporary checksum around the B’s fields that matter to calculate and store the value as a new B field. Your post_save then can recalculate the checksum to see whether A.calculate() needs to run again or not.

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

Sidebar

Related Questions

I have a model, smth like this: class Action(models.Model): def can_be_applied(self, user): #whatever return
Model: class Session(models.Model): tutor = models.ForeignKey(User) start_time = models.DateTimeField() end_time = models.DateTimeField() def __unicode__(self):
So, say I have models like this: class Foo(Model): name = CharField(max_length=200) def latest_comment(self):
I have a model that looks like this class RSVP (models.Model): def __unicode__(self): return
I have an issue where I have something like this: class Thing(models.Model): def can_vote(self,
Example models: class Parent(models.Model): name = models.CharField() def __unicode__(self): return self.name class Child(models.Model): parent
My model: class Player(models.Model): player_name = models.CharField(max_length=50) player_email = models.CharField(max_length=50) def __unicode__(self): return self.player_name
In my models.py class Alert(models.Model): user = models.CharField(max_length=30, blank=True) a = models.IntegerField(blank=True) def __unicode__(self):
I have a memoized Django model manager method as follows: class GroupManager(models.Manager): def get_for_user(self,
I have 03 models book->target_readers and book has collection. class Book(models.Model): def __unicode__(self): return

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.