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)
2条答案
按热度按时间vmdwslir1#
最后我写了一个基于
populate_history
的修改过的脚本。它识别所有没有历史记录的对象,并将它们添加到历史表中。下面是一个简单的版本(没有批处理)。sg3maiej2#
受@beldaz和the tests in simple-history的启发,我简单地调用populate_history作为迁移操作