etianen / django-reversion

django-reversion is an extension to the Django web framework that provides version control for model instances.
https://django-reversion.readthedocs.io
BSD 3-Clause "New" or "Revised" License
3.04k stars 489 forks source link

'revision.revert(delete=True)' should delete existing objects not present in the version to which we are reverting, but its not deleting the extra objects #975

Open Prakhar1247 opened 3 months ago

Prakhar1247 commented 3 months ago

I have three models defined as:-

@reversion.register()
class Transaction(models.Model):
    trackid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    data_group = models.CharField(max_length=255)

@reversion.register()
class Transaction_Quest(models.Model):
    trackid = models.ForeignKey(Transaction, on_delete=models.CASCADE, related_name='transaction_records')
    questionid = models.ForeignKey(Questions_List, on_delete=models.CASCADE, related_name='question_details')
    question_status = models.CharField(max_length=10)
    question_value = models.DateField(default=None, null=True)

@reversion.register()
class Transaction_Ques_Attachment(models.Model):
    trackid = models.ForeignKey(Transaction, on_delete=models.CASCADE, related_name='transaction_id')
    ques_id = models.ForeignKey(Transaction_Question, on_delete=models.CASCADE, related_name='trans_ques_records')
    filename = models.CharField(max_length=50)
    file_description = models.TextField()

A Transaction can be associated to multiple Questions (Transaction_Quest) and a Question can be associated to multiple attachments (Transaction_Ques_Attachment)

So when I am creating a new entry for these models under with reversion.create_revision(): block, so a new version is created for each of the three models with same revision id. Example:- A user created a record containing:-

So in total there are 5 version entries created with same revision_id = 1 as- 1 version for Transaction model, 2 versions for Transaction_Quest model and 2 versions for Transaction_Ques_Attachment model.

Updating same record under with reversion.create_revision(): block, so again a new version is created for each of the three models with same revision id as below:- Now this time user updated the above record and this time he added 2 more new objects in the Transaction_Ques_Attachment model associated to same 1st object (id=1) of Transaction_Quest model and updated some data in the object of Transaction model and 2nd object of Transaction_Quest model. So now this time 7 version entries created with same revision_id = 2 which now is also the selected version. 1 version for Transaction model, 2 versions for Transaction_Quest model and 4 versions for Transaction_Ques_Attachment model associated to 1st object (id=1) of Transaction_Quest model.

Logic to revert back to any specific version

Now for reverting to previous version i.e. revision_id=1 from revision_id=2, I am using below logic and I have passed delete=True:-

from reversion.models import Revision
revision = Revision.objects.get(id=1)
revision.revert(delete=True)

So after reverting to previous version i.e. revision_id=1 extra 2 objects present in revision_id=2 for model Transaction_Ques_Attachment should be deleted as version 1 does not have these objects. But even after using delete=True option those extra 2 objects are not getting deleted from the Transaction_Ques_Attachment model. Though other records are reverted back to version 1 but the extra 2 records which were added in version 2 are still there in the Transaction_Ques_Attachment model even after reverting back to Version 1.

So need help on this issue as to why revision.revert(delete=True) is not working as expected and why the extra records not getting deleted from the Transaction_Ques_Attachment model even after reverting back to 1st revision?