models.py 1.0 KB

123456789101112131415161718192021222324252627282930313233343536
  1. from django.db import models
  2. from extras.models import ObjectChange
  3. from utilities.utils import serialize_object
  4. class ChangeLoggedModel(models.Model):
  5. """
  6. An abstract model which adds fields to store the creation and last-updated times for an object. Both fields can be
  7. null to facilitate adding these fields to existing instances via a database migration.
  8. """
  9. created = models.DateField(
  10. auto_now_add=True,
  11. blank=True,
  12. null=True
  13. )
  14. last_updated = models.DateTimeField(
  15. auto_now=True,
  16. blank=True,
  17. null=True
  18. )
  19. class Meta:
  20. abstract = True
  21. def to_objectchange(self, action):
  22. """
  23. Return a new ObjectChange representing a change made to this object. This will typically be called automatically
  24. by extras.middleware.ChangeLoggingMiddleware.
  25. """
  26. return ObjectChange(
  27. changed_object=self,
  28. object_repr=str(self),
  29. action=action,
  30. object_data=serialize_object(self)
  31. )