如何使用django更新会计应用程序中的余额? [英] How to update balance in a accounting app with django ?

查看:208
本文介绍了如何使用django更新会计应用程序中的余额?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习Django,试图做一个会计应用程序来跟踪我的费用等。

I'm learning Django, trying to do an accounting app for keeping track of my expenses, etc..

我创建了两个模型的数据库,一个帐户和一个操作。
但我不知道如何保持我的平衡更新每个操作。

I created the database with two models, one for accounts and one for operations. But i have no idea how to keep my balance updated with each operation.

我想也许,每次我保存一个新的操作,我更新通过覆盖操作模型的保存方法来平衡?但在这种情况下,如果我犯了一个错误,必须删除一个操作,我的余额将不会被更新,对吧?

I was thinking maybe, each time i save a new operation, I update the balance by overriding the save method of Operation Model ? But in that case, if i make a mistake and have to delete one operation, my balance won't be updated then, right ?

我想我也可以创建一个BalanceHistory模型,其中包含所有余额的历史记录,但是删除操作时会出现同样的问题。

I thought I could also create an BalanceHistory model, with an history of all balances but then same problem in case of deleting an operation..

我看到的最好的选择是动态地更新我的余额,每次我想显示它,添加所有的操作,当时..但我不知道我该怎么做。

The best option i see would be to update dynamically my balance each time i want to display it, adding all the operations at that time.. but i don't see how i could do that..

所以,如果有人对此有一些洞察,那将是巨大的。
我知道这不是一个纯粹的Django相关的问题,但因为我使用Django,它会更好,如果我可以找到一个想法使用Django功能!

So if anyone has some insight about that, that would be great. I know it is not a purely Django-related issue, but as i'm working with Django, it would be better if i can find an idea using Django features !

感谢您的时间和帮助!

推荐答案

你是对的,这是棘手的 - 没有办法保证一个准确的余额,除非根据帐户的整个历史记录,每次读取动态计算。

You're right that this is tricky--there isn't a way to guarantee an accurate balance except by dynamically calculating it on every read based on the entire history of the account.

如果您选择这样做,您可以使用Django ORM的聚合特性来计算操作的总和。例如,如果操作有一个名为 amount 的字段(表示该操作的余额变化的正数或负数),您可以计算余额,如: p>

If you choose to go that way, you can use the Django ORM's aggregation features to calculate a sum of the Operations. For example, if you operation had a field called amount (a positive or negative number indicating the change in balance for that operation), you could could calculate the balance like:

Operation.objects.filter(account=my_account).aggregate(balance=Sum('amount'))

如果你不想因为任何原因这样做,你也可以使用自定义 save 方法。您可以使用 delete 方法 save 来处理个别项目的删除。

If you didn't want to do that for whatever reason, you could also maintain a running balance using a custom save method as you've described. You could use the delete method like save to handle individual item deletions.

但是,如果您曾经执行批量插入,更新或删除或原始SQL操作,那么该方法将会导致问题,因为这些操作不会调用 save delete 方法。但是,如果您需要使用这些功能,您可以使用混合方法 - 使用 save delete 选项,并触发一个完整的重新计算与聚合周期性地纠正错误或立即执行操作后,你知道会拧平衡。

However, that method will cause problems if you ever user bulk inserts, updates, or deletes or raw SQL operations, since those will not invoke the save and delete methods. However, if you need to use those features, you might be able to use a hybrid method--use the save and delete options when doing one-off transactions, and triggering a full recalculation with aggregation periodically to fix errors or immediately after doing an operation you know will screw up the balance.

这篇关于如何使用django更新会计应用程序中的余额?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆