2

I am using django-simple-history to track changes to some fields on my models.

My simplified model setup:

class Collection(models.Model):
    description = models.TextField()
    # ...
    history = simple_history.HistoricalRecords()


class Item(models.Model):
    content = models.TextField()
    collection = models.ForeignKey(Collection, related_name="items", on_delete=models.CASCADE)
    # ...
    history = simple_history.HistoricalRecords()

Now ideally I would like to see Item changes reflected in Collection.history.

For example, if a new Item is added to a collection, I would like to have a new entry in Collection.history.

Is this somehow possible?

EDIT: some more details on what I want to do: Basically whenever something inside a Collection changes, I would like to have a snapshot to be able to do diffs.

So for example, the description is changed, then I can simply diff between the two snapshots to see what changed. This works well with simple-history.

Now what doesn't work is that whenever an Item is changed/added/removed, the new state is not reflected in the history of my Collection. Consider:

c = Collection.objects.get(id=123)
item = Item.objects.get(id=456)

c.items.add(item)

If I check c.history, there is no way to know that item was added to the collection as far as i can tell.

Blub
  • 3,762
  • 1
  • 13
  • 24
  • It stores the historical items (together with some fields about when it was changed, and by what user). The question is not entirely clear to me, since you do not really specify *what* you aim to do. – Willem Van Onsem Jun 09 '21 at 15:15
  • @WillemVanOnsem I added some more details, is it a bit more clear like this? – Blub Jun 09 '21 at 15:26
  • you can define a model that is a junction table (`through=...` in Django https://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.ManyToManyField.through), and then make that `through` table also a historical model. – Willem Van Onsem Jun 09 '21 at 15:28

0 Answers0