Skip to content

Instantly share code, notes, and snippets.

@i
Created July 19, 2018 18:05
Show Gist options
  • Save i/1ac4641283d059ae736e71487f6d2479 to your computer and use it in GitHub Desktop.
Save i/1ac4641283d059ae736e71487f6d2479 to your computer and use it in GitHub Desktop.
class Snapshot(models.Model):
# unique identifier for snapshots
id = models.Charfield(max_length=32)
# the record which initiated the snapshot
root_id = models.CharField(max_length=32)
@transaction.atomic
def restore(self):
[x.restore() for x in self.snapshot_records.all()]
self.delete()
class SnapshotRecord(models.Model)
snapshot = models.ForeignKey(Snapshot, null=False)
# this field is used to represent the record affected by the delete
record_id = models.CharField(max_length=32)
# these fields are used to represent links to another record.
# if they are set, it means a
fk_field = models.CharField(max_length=64, null=True)
fk_value = models.CharField(max_length=64, null=True)
@transaction.atomic
def restore(self):
# we'll implement BaseModel.get later
record = BaseModel.get(self.record_id, include_deleted=True)
if fk_model is None:
record.is_deleted = False
else:
setattr(record, self.fk_field, self.fk_value)
record.save()
self.delete()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment