In django - is there a default timestamp field for all objects? That is, do I have to explicitly declare a 'timestamp' field for 'created on' in my Model - or is there a way to get this automagically?
7 Answers
No such thing by default, but adding one is super-easy. Just use the auto_now_add
parameter in the DateTimeField
class:
created = models.DateTimeField(auto_now_add=True)
You can also use auto_now
for an 'updated on' field.
Check the behavior of auto_now
here.
For auto_now_add
here.
A model with both fields will look like this:
class MyModel(models.Model):
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)

- 900
- 1
- 7
- 22

- 3,107
- 2
- 21
- 23
-
For the "authetic" experience of timestamp field I add `editable=False` and recommend such, eg: `created = models.DateTimeField(auto_now_add=True, editable=False)`. – Williams Aug 21 '21 at 22:11
-
1Note that using the `auto_now` or `auto_now_add` options causes the field to also have `editable=False`, so you don't need to specify it explicitly. – MoshiBin Dec 09 '21 at 00:01
Automagically doesn't sound like something django would do by default. It wouldn't force you to require a timestamp.
I'd build an abstract base class and inherit all models from it if you don't want to forget about the timestamp / fieldname, etc.
class TimeStampedModel(models.Model):
created_on = models.DateTimeField(auto_now_add=True)
class Meta:
abstract = True
It doesn't seem like much to import wherever.TimeStampedModel
instead of django.db.models.Model
class MyFutureModels(TimeStampedModel):
....

- 115,817
- 29
- 282
- 245
-
3There is also `model_utils` for Django which contains already `TimeStampedModel`, but if you don't need anything else from this package, just keep it w/o extra weight. – boldnik Oct 28 '13 at 10:05
-
-
Pretty cool... I have 3 classes that needed a timestamp for `created` and `updated`. This is a great way of doing it cleanly. – ron_g Feb 12 '20 at 20:13
If you are using django-extensions (which is a good app for adding functionality to the django-admin.py command line helper) you can get these model fields for free by inheriting from their TimeStampedModel or using their custom TimeStamp fields

- 53,000
- 18
- 155
- 177
You can try django-extensions
if you want to use time-stamp abstract model
from django_extensions.db.models import TimeStampedModel
class A(TimeStampedModel):
...
It has other abstract models. you can use that too.

- 4,202
- 5
- 39
- 62
If you want to be able to modify this field, set the following instead of auto_now_add=True:
For Date
from datetime import date
models.DateField(default=date.today)
For DateTime
from django.utils import timezone
models.DateTimeField(default=timezone.now)

- 1,091
- 1
- 10
- 18
Since you might be using IDs with your models, it's safe to use them this way.
from django.db import models
from uuid import uuid4
class TimeStampedModel(models.Model):
id = models.UUIDField(primary_key=True, default=uuid4, editable=False)
date_created = models.DateTimeField(auto_now_add=True)
date_updated = models.DateTimeField(auto_now=True)
class Meta:
abstract = True
Then import the TimeStampedModel in any model you want to use them, eg
class Detail(TimeStampedModel):
first_name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100)
class Meta:
verbose_name = "Detail"
verbose_name_plural = "Details"

- 199
- 1
- 10
I think i would go with
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
I need some clarifications on using
class TimeStampedModel(models.Model):
created_on = models.DateTimeField(auto_now_add=True)
class Meta:
abstract = True

- 16,086
- 4
- 44
- 68

- 11
- 3