gpt4 book ai didi

django - 使用 Django Simple History 创建历史记录的问题

转载 作者:行者123 更新时间:2023-12-05 09:15:06 33 4
gpt4 key购买 nike

为什么 Django-Simple 历史记录在调用保存方法时创建,如果我调用更新然后它不创建历史记录?

Django:1.11.15Django-简单历史:1.9.0 python :3.6

最佳答案

documentation 中所写这是一个已知问题:

Django Simple History functions by saving history using a post_savesignal every time that an object with history is saved. However, forcertain bulk operations, such as bulk_create and queryset updates, signals are not sent, and the history is not savedautomatically. However, Django Simple History provides utilityfunctions to work around this.

基本上,应用程序利用了您 .save() 模型这一事实,而这被一些 ORM 调用规避(因为这样您就无法执行数据库级别的“批量”操作)。

而不是使用

Entry.objects.filter(pub_date__year=2010).update(comments_on=False)

因此您需要执行:

for e in Entry.objects.filter(pub_date__year=2010):
e.comments_on = False
e.save()

bulk_create 有一个变体:bulk_create_with_history ,从那时起它就简单地进行了两次批量创建:一次用于对象,一次用于“历史记录”。

关于django - 使用 Django Simple History 创建历史记录的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53031661/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com