将 django 模型的编辑历史存储在另一个自定义模型中
我有两个模型可以说:
class superfields(Model):
fieldA = models.FloatField()
fieldB = models.FloatField()
class Meta:
abstract = True
class my_model( superfields ):
def has_history( self ):
return self.my_model_history_set.count() > 0
class my_model_history( superfields ):
reason = models.TextField()
mymodel = models.ForeignKey( my_model )
“my_model”填充了数据(在 fieldA 和 fieldB 下)。每当有人编辑“my_model”的字段并保存时,我不想保存此模型中的更改,而是希望将其存储为新行,其中所有值都在“my_model_history”中,此外还有“原因”字段,而“my_model”数据保持不变。
在自定义模板、自定义视图、模型管理等方面处理这种情况的最佳方法是什么?我做得正确吗?
为了让我的上述问题有一定的意义,在我的项目中,“my_model”下的数据本质是市场价格,我需要维护所有曾经编辑过的市场价格的历史记录,并附上编辑的“原因”。
I have two models lets say:
class superfields(Model):
fieldA = models.FloatField()
fieldB = models.FloatField()
class Meta:
abstract = True
class my_model( superfields ):
def has_history( self ):
return self.my_model_history_set.count() > 0
class my_model_history( superfields ):
reason = models.TextField()
mymodel = models.ForeignKey( my_model )
'my_model' is populated with data (under fieldA and fieldB). Whenever someone edits 'my_model's fields and saves, I don't want to save the change in this model but want to store it as a new row with all values in 'my_model_history', in addition to a 'reason' field while 'my_model' data stays the same.
What is the best way to approach this scenario in terms of custom templates, custom views, model admins etc etc. Am I doing it correctly?
To give my question above some sense, in my project, the nature of data under 'my_model' is market prices and I need to maintain a history of all the market prices ever edited with a 'reason' for the edit.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我的解决方案:
是的。我遵循的一个简单快速的解决方案如下:
我创建了三个与此类似的模型:
我设置了一个信号:
并在保存到 my_model_history 之前由 pre_save() 信号调用“创建历史记录”:
每当用户单击 my_model 行进行编辑时,我都会使用“my_model_history”来生成我的编辑表单并使用用户所选行中的值填充它。 (已经编写了一个视图和模板来执行此操作)
因此编辑表单现在将具有:
my_model 数据行
my_model 数据行
用户现在可以编辑 fieldA/fieldB。输入原因。按保存触发上面的信号。
在保存之前,
主模型(旧值)和
历史模型(新值)
(使用新值)。
历史模型(使用旧值)
有一个理由。
希望有帮助。如果还有任何其他问题,请告诉我。
My Solution:
yes. A simple and quick solution I am following is as follows:
I create three models similar to this:
I've setup a signal:
and 'create history' to be called by the pre_save() signal before saving in my_model_history:
Whenever user clicks on a my_model row for editing, I use 'my_model_history' to generate my edit form and populate it with the values from the user selected row. (Have written a view and template to do that)
So the edit form will now have:
my_model data row
my_model data row
User can now edit fieldA/fieldB. Enter a reason. Press save to trigger the signal above.
Before saving,
the main model(old values) and
history model(New values)
(with the new values).
history model (with the old values)
with a reason.
Hope it helps. Let me know if there are any further questions.
为什么不使用该条目作为表单的初始数据来创建新实例,而不是编辑现有条目?新对象被保存,原始对象保持不变......
Instead of editing an existing entry, why not use that entry as initial data for a form to create a new instance? The new object gets saved, the original stays the same...
我在“pro Django”一书中第 264 页找到了关于保留详细编辑历史记录的解释。通读完之后,我将尝试实现我需要的内容。完成后将在这里发布我的方法
I found an explanation on keeping detailed edit histories in the book 'pro Django' page 264. After a read through I'll try an implementation of what I need. Will post my approach here when I'm done