1

After setting up history for a model using django-simple-history, I wanted to run populate_history to populate the history table based on the existing contents of the table. However, other users have already made a number of changes, causing the history table to be partially populated. Running populate_history --auto simply results in message Existing history found, skipping model.

I wish to retain the existing history, but populate history for all records not currently stored in the history. Is there a way to do this?

4

1 回答 1

0

I ended up writing a modified script based on populate_history. It identified all objects with no historical records, and added them in to the history table. A simple version (no batching) is below.

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)
于 2022-02-03T00:52:25.900 回答