-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseModel.py
More file actions
42 lines (30 loc) · 1.29 KB
/
BaseModel.py
File metadata and controls
42 lines (30 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import uuid
from django.db import models
from django.db.models import Index
from django.utils import timezone
from commons.models.BaseModelManager import BaseModelManager
class BaseModel(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
deleted_at = models.DateTimeField(null=True, blank=True)
# Set the default manager to exclude soft-deleted objects
objects = BaseModelManager()
all_objects = models.Manager()
class Meta:
abstract = True
indexes = [
# Explicitly define an index for the deleted_at field
Index(fields=["deleted_at"]),
]
"""Perform a soft delete by setting the deleted_at field."""
def delete(self, using=None, keep_parents=False):
self.deleted_at = timezone.now()
self.save()
"""Restore a soft-deleted record by clearing the deleted_at field."""
def restore(self):
self.deleted_at = None
self.save()
"""Perform a hard delete by permanently removing the record from the database."""
def hard_delete(self, using=None, keep_parents=False):
super(BaseModel, self).delete(using=using, keep_parents=keep_parents)