问题描述
为简化我的问题,假设我有一个简单的用户模型,恰好有一个 IntergerField
包含游戏中的得分。
To simplify my problem, let's say I have a simple User model that happen to have an IntergerField
containing a score in a game.
我想每次修改核心时都存储时间和分数值。例如,假设用户A在时间= 0时以分数= 0开始,然后在时间= 3时赢得5分。我会在视图中更改分数的值,但我也想能够查询另一个属性,该属性会给我 {0:0,3:5}
I'd like to store the time and the value of the score every time the core is modified. For example, let's say user A starts with score=0 when time=0, then wins 5 points at time=3. I'd change in the view the value of the score, but I'd also like to be able to query another attribute that would give me {0:0, 3:5}
该应用将需要与MySQL,PostgresSQL以及可能的SQLite一起使用
The app will need to be used with MySQL, PostgresSQL and probably SQLite
推荐答案
对得分使用单独的模型,至少具有值
和 time
字段以及一个fkey您的用户模型。然后通过以下方式从用户访问当前分数:
Use a separate model for the score, having at least value
and time
fields and a fkey to your user model. Then access the current score from the user by:
user.score_set.order_by('-time').first()
这篇关于如何在Django中存储变量历史记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!