models.py 1.1 KB

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