在使用django-simple-history为模型建立历史记录之后,我希望运行populate_history来根据表的现有内容填充历史表。但是,其他用户已经做了一些更改,导致部分填充历史表。运行populate_history --auto只会导致消息Existing history found, skipping model。
我希望保留现有的历史记录,但是填充历史记录中目前没有存储的所有记录。有办法这样做吗?
发布于 2022-02-03 00:52:25
最后,我编写了一个基于populate_history的修改脚本。它标识所有没有历史记录的对象,并将它们添加到历史表中。下面是一个简单的版本(没有批处理)。
from django.apps import apps
from simple_history.utils import get_history_manager_for_model, get_history_model_for_model
def populate_model_history(model):
history = get_history_model_for_model(model)
history_manager = get_history_manager_for_model(model)
# Insert historical records for objects without existing history
# NOTE: A better approach would be to do this in batches, as in populate_history.py
for instance in model.objects.exclude(pk__in=history.objects.values_list(model._meta.pk.name)):
history_manager.bulk_history_create([instance], batch_size=1)
model = apps.get_model('app', 'my_model')
populate_model_history(model)https://stackoverflow.com/questions/70932848
复制相似问题