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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T19:55:37+00:00 2026-05-11T19:55:37+00:00

I need to perform a range of simple calculations on an Invoice model object,

  • 0

I need to perform a range of simple calculations on an Invoice model object, which has a number of Order children associated with it that hold the order quantity and order value etc. This is all managed through the admin form for Invoice (with order as inlines)

The code I’m working with now performs these calcs like this:

Invoice (models.py)

def save():
    #get the total of all associated orders
    for order in self.invoice_orders.all():
    self.invoice_net_total += order.order_value
    super(Invoice, self).save()

Which causes a few problems when changing a child order quantity and then saving the form – I get the previous total instead of the new total, only when I save again does the total correct itself. Perhaps due to the way Django saves parent and child objects?

Other option I toyed with was moving this calculation to the child order object (prepare for horrible code):

Order (models.py)

def save():
  if not self.id:
    self.invoice.invoice_net_total += self.order_value
  elif self.id:
    #grab old instance
    old = Order.objects.get(pk=self.id)
    #remove that old total
    self.invoice.invoice_net_total -= old.order_value
    self.invoice.save()
    #add new total
    self.invoice.invoice_net_value += self.order_value
    self.invoice.save()

Though that’s not very effective either.

Wondering if anyone could guide me to a straightforward way of ensuring these calcs perform as they should? Thought of signals (relatively new to me) but wondered if that was overkill. I’m probably overthinking this!

Thank you

  • 1 1 Answer
  • 2 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-11T19:55:37+00:00Added an answer on May 11, 2026 at 7:55 pm

    On a different thought, consider not even explicitly saving the invoice total. Instead, it may be dynamically recalculated each time you query for an invoice.

    I think this is better modeling as it avoid redundant and possibly inconsistent data. SQL and Django are smart enough to compute you invoice total without much overhead each time you need it. Esp. you don’t do the summation in you program if you use an aggregation function

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

Sidebar

Related Questions

I am having a table which has about 17 fields. I need to perform
What steps I need to perform in order to convert asp.net 2 application from
My company is looking into writing a custom application that will need to perform
I need to perform a calculation in SSAS which only applies to the current
In my script I need to perform a set of actions through range of
I need to perform google search to retrieve the number of results for a
I need to perform some action when my application receives focus. I've tried hooking
I need to perform Diffs between Java strings. I would like to be able
I need to perform a filtered query from within a django template, to get
I need to perform a HTTP GET from PHP. More specifically, from within /index.php

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.