17

having used cakephp in the past, one thing (perhaps the only thing?) i liked about it was that it had a "create" and "update" timestamp capability that was lovely - simply put, when you first added an item, the "create" date was set (assuming you named it right - create_date, i think)

Anytime thereafter, if an update was performed, the "update" field was set to the current time.

Does django have this as well? If so, what/how do i name the fields to get it to pick them up?

bharal
  • 15,461
  • 36
  • 117
  • 195
  • 1
    Possible duplicate of [In django do models have a default timestamp field?](http://stackoverflow.com/questions/8016412/in-django-do-models-have-a-default-timestamp-field) – Hardik Sondagar Sep 07 '16 at 10:37

2 Answers2

32

It is not added to your model built-in in every table. You must add it as field to your model.

class Message(models.Model):

    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

Message in this case your table's name.

Sayonara
  • 337
  • 4
  • 8
23

Sure it has!

Check auto_now and auto_now_add in the doc

okm
  • 23,575
  • 5
  • 83
  • 90
  • 2
    From the [source](https://docs.djangoproject.com/en/dev/_modules/django/db/models/fields/#DateTimeField), it looks like they're mutually exclusive. But could you help to explain the difference? It isn't very clear what is the difference between `auto_now` and `auto_now_add`. – alvas Jun 09 '16 at 03:48
  • 3
    Maybe [this](https://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.DateField.auto_now) clear up more. `auto_now` is generated on each edit, `auto_now_add` only generated on creation. – bizi Sep 08 '16 at 22:40