Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrating existing data to start capturing history #179

Open
naomicrosthwaite opened this issue Dec 12, 2024 · 1 comment
Open

Migrating existing data to start capturing history #179

naomicrosthwaite opened this issue Dec 12, 2024 · 1 comment

Comments

@naomicrosthwaite
Copy link

Hi there 👋

I have an existing model that has existing data that I would like to start tracking history on. I found that by editing an instance for the first time after adding history that the event created didn't capture a diff, so I decide to create dummy "insert" events utilising create_event() as part of the migration file generated by adding the @pghistory.track decorator to the mode.

This worked fine locally however when deploying I've got the following error: ""insert" is not a registered tracker label for model MyModel". Am I doing something wrong? The "insert" label is the label of a newly created MyModel object after the history tracking was set up.

Relevant code below:

models.py

@pghistory.track()
class MyModel(models.Model):
    ...
0123_migration_file.py

def create_dummy_insert_events(apps, schema_editor):
   MyModel = apps.get_model("myapp", "MyModel ")
   for mymodel in MyModel .objects.all():
       pghistory.create_event(mymodel , label="insert")

class Migration(migrations.Migration):
      migrations.CreateModel(
           name="MyModelEvent",
           ...
      migrations.RunPython(create_dummy_insert_events, migrations.RunPython.noop), 

Thanks in advance!

@naomicrosthwaite
Copy link
Author

I since found issue #94 which I found useful and allowed me to make progress. I would like to understand further why you'd need to reference the model itself and not through the app registry, especially since this is as recommended by the Django docs? 🤔

I also personally would think it would be nice functionality to default create those initial Event objects for any existing data (or at least configurable in case you didn't want it). Or instead update so that the first update Event logged on an existing instance captures a diff. Happy to make this a discussion if it's better placed as one 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant