Share Django timestamps and UUIDs with abstract models
Use small abstract models for repeated fields, then account for the save paths that do not update timestamps. UUIDs change identifiers, not authorization.
An abstract Django model contributes fields to each concrete subclass without creating a table of its own. It is a good fit for a small convention such as creation and modification timestamps. Keep the convention narrow: a base class inherited everywhere becomes expensive to change.
Define only the fields you need
In an installed Django application’s models.py:
import uuid
from django.db import models
class TimestampedModel(models.Model):
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
class Meta:
abstract = True
class UUIDModel(models.Model):
id = models.UUIDField(
primary_key=True,
default=uuid.uuid4,
editable=False,
)
class Meta:
abstract = True
class Project(TimestampedModel, UUIDModel):
name = models.CharField(max_length=255)
description = models.TextField(blank=True)
Pass uuid.uuid4, the callable, rather than uuid.uuid4(), one value evaluated when the module loads. Each new object then receives its own identifier. The concrete Project table contains both timestamp fields and its UUID primary key; the two abstract classes have no separate rows to join.
After adding these models, generate and review the migration before applying it:
python manage.py makemigrations
python manage.py migrate
For an existing table, changing its primary key is a data and foreign-key migration, not just a base-class refactor. Plan that change separately. New models can use this pattern without rewriting existing identifiers.
Know which writes update the timestamp
auto_now is applied during normal field pre-save processing. It is not a database trigger. In particular, QuerySet.update() bypasses model save() and does not automatically refresh updated_at.
from django.utils import timezone
from .models import Project
Project.objects.filter(name="Prototype").update(
name="Released",
updated_at=timezone.now(),
)
Likewise, if you call save(update_fields=["name"]), the timestamp field is excluded. Include "updated_at" in that list if the modification should refresh it. Bulk operations and external SQL need an explicit timestamp policy too.
auto_now_add records insertion through Django rather than a historical event time supplied by the caller. If importing original creation dates is a routine requirement, a field with default=timezone.now may fit better; it permits an explicit value while still supplying a default.
Keep ordering and access policy explicit
Do not add global ordering to the base class merely because some views display the newest items first. Default ordering affects many queries and may add a sort where none is needed. For a particular listing, use an explicit order such as order_by("-created_at", "id"); the second field gives deterministic ties.
UUIDs are useful for identifiers created independently, but they use more storage than typical integer keys. An unpredictable identifier also does not grant access: every lookup still needs authorization.
The abstract-model documentation, field reference and QuerySet update documentation describe the relevant behavior.