1
# Copyright 2012 Canonical Ltd. This software is licensed under the
2
# GNU Affero General Public License version 3 (see the file LICENSE).
4
"""Model base class with creation/update timestamps."""
6
from __future__ import (
18
from django.db import connection
19
from django.db.models import (
23
from maasserver import DefaultMeta
27
"""Current database time (as per start of current transaction)."""
28
cursor = connection.cursor()
29
cursor.execute("select now()")
30
return cursor.fetchone()[0]
33
class TimestampedModel(Model):
34
"""Abstract base model with creation/update timestamps.
36
Timestamps are taken from the database transaction clock.
38
:ivar created: Object's creation time.
39
:ivar updated: Time of object's latest update.
42
class Meta(DefaultMeta):
45
created = DateTimeField(editable=False)
46
updated = DateTimeField(editable=False)
48
def save(self, *args, **kwargs):
51
self.created = current_time
52
self.updated = current_time
53
return super(TimestampedModel, self).save(*args, **kwargs)