Django – Automatic creation date and update date

In your model class, we can set the auto_now_add and auto_add in the DateField or DateTimeField for creation date and update date.

from django.db import models

class Person(models.Model):
  name       = models.CharField(max_length=256)
  homepage   = models.URLField(max_length=256)
  created_at = models.DateTimeField(auto_now_add=True)
  updated_at = models.DateTimeField(auto_now=True)

  def __unicode__(self):
    return self.name

 

The auto_now_add will make the field read only while the auto_now will refresh the the field value when a .save() is executed.

Done =)

Reference: Django documentation – DateField

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s