Articles → DJANGO → auto_now and auto_now_add in Django

auto_now and auto_now_add in Django






Purpose








AttributeValueImpact
auto_nowTrueAutomatically updates the field with current date and time when data is saved.
auto_nowFalseDate and time control is displayed on the page.
auto_now_addTrueAutomatically updates the field with current date and time when data is created.
auto_now_addFalseDate and time control is displayed on the page.





Example




from django.db import models

# Create your models here.
class MyNewScreen(models.Model):
    name = models.CharField(max_length = 120)
    created = models.DateTimeField(auto_now_add=True, null = True)    




Picture showing the new screen for adding a new record

Click to Enlarge



Picture showing the record added in the database

Click to Enlarge





Picture showing the new screen with the date time field

Click to Enlarge


Posted By  -  Karan Gupta
 
Posted On  -  Wednesday, January 8, 2020

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250