I'm working on a django project and i'd like to developp an history application to show every changes in my models. I installed the package django-simple-history that seems to be the easier and the most used.
The thing is, that it raises an error when i try to delete an object involved in the ForeignKey of a child Model.
Here is an example:
class ParentModel(models.Model):
age = models.IntegerField()
name= models.CharField(max_length = 30, blank=True)
history = HistoricalRecords()
class ChildModel(models.Model):
parent_objects = models.HistoricForeignKey(ParentModel, on_delete=models.CASCADE)
book = models.CharField(max_length = 20, blank=False)
history = HistoricalRecords()
So i have to tables, and one refers to the other by a ForeignKey (i used the package's HistoricForeignKey in my code but it's the same).
Then i access to the historic objects like this:
from django.apps import apps
apps.get_model("ParentModelHistorical").objects.all()
apps.get_model("ChildrenModelHistorical").objects.all()
The problem is when i delete a ParentModel's object. So all the related ChildrenModel's objects are also delted (the CASCADE deletion).
And when i try to access again the historic objects i get this :
apps.get_model("ParentModelHistorical").objects.all() #-> no errors, everything's fine
apps.get_model("ChildrenModelHistorical")..objects.all() #-> ERROR : MyApp.models.parent_object.DoesNotExist: parent_object matching query does not exist.
So what i understand is that the historic object of the Child Model is raising an error because it was created after the parent object was deleted. And so it's foreignKey attribute links on nothing.
I can’t do without cascading so I hope it’s possible to overcome this error because django-simple-history has met all my expectations so far.
Thanks you for your help