There are several ways and it depends a lot on what your goal is and how you want to implement it:
Implement an abstract class for auditing:
You can write an abstract class that has your audit fields, and the table will be created automatically with those fields, that is.
class AudtoriaMixin(models.Model):
created_at = models.DateTimeField(auto_now_add=True)
created_by = models.CharField(max_length=255, blank=True, editable=False)
modified_at = models.DateTimeField(auto_now=True)
modified_by = models.CharField(max_length=255, blank=True, editable=False)
class Meta:
abstract = True
class Usuario(AudtoriaMixin):
name = models.CharField(max_length=255)
Create a signal that captures the saved and deleted events and registers them in a table.
The advantage of using this form is that it is not intrusive, you do not have to modify the models to use it, the disadvantage is that you can not customize some extra parameters, like the user you are modifying.
@receiver(post_save)
def audit(sender, **kwargs):
obj_str = str(kwargs.get('instance'))
Auditoria(instancia = obj_str, accion="guardado").save()
Use a AuditTrail
field:
You can copy the snippet from link and use it to save an audit field for any model.
from django.db import models
import audit
class Usuario(models.Model):
first_name = models.CharField(max_length=255)
last_name = models.CharField(max_length=255)
salary = models.PositiveIntegerField()
history = audit.AuditTrail()
Use an external library to audit and save history:
Finally you can use any of the applications that the django packages universe offers: link