django模型对象属性按日期跟踪/更新/更改 [英] django model object attribute track updates / edits / changes by date

查看:234
本文介绍了django模型对象属性按日期跟踪/更新/更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我有一个Django模型类.

I have a django model class e.g.

class BankAccount (model.Models):
    value = models.PositiveSmallIntegerField(default=0)

我想编辑该值并每周进行编辑,例如

I would like to edit this value and edit it on a weekly basis e.g.

wk1:值= 1 wk2:值= 30 ... 第50周:值= 70

wk1: value = 1 wk2: value = 30 ... wk 50: value = 70

问题:

我想跟踪值的变化并能够显示前几周的值:

I would like to keep track of the value changes and be able to display the value for the previous weeks:

例如今天是40周,价值= 60,我要检查30周,20周,10周等的值是什么...

e.g. today is wk 40 and value = 60 and I would like to check what was the value in wk 30, wk 20, wk 10 etc ...

如何在Django中做到这一点?

How to do this in django?

我目前唯一的想法是要么每隔一周创建一个新对象并保留所有对象,要么使用现成的应用程序跟踪模型中的更改?

My only idea I have at the moment is to either create a new object for every other week and keep all the objects OR track the changes in the model with off-the-shelf app?

有人有更好的主意吗?

推荐答案

对我来说,有一个transaction模型及其日期和时间来存储每个值很有意义,然后根据需要更新BankAccount值.

To me it would make sense to have a transaction model with dates and times where you store each value then update the BankAccount value as appropriate.

您应将交易应用于交易表并更新BankAccount的余额/值.

You would apply transactions to the transactions table and update the balance/value of BankAccount.

这将是设计类似内容的标准方法.它还为您提供了一个安全网,历史记录应在出现问题时允许重新计算值等.

This would be the standard way to design something like this. It also gives you a safety-net, history should something go wrong allowing a recalculation of of the value etc.

另一个选择是仅具有transactions模型,但具有附加字段current_balance.这将用于存储在该交易执行后立即生效的余额.因此,用户和货币的最新交易记录也包含其当前余额.

The other option is to just have a transactions model but with an additional field current_balance. This would be used to store the balance effective right after that transaction executed. Thus, the latest transaction record for a user and currency also contains their current balance.

这篇关于django模型对象属性按日期跟踪/更新/更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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