24 – HOW TO ADD & WORK WITH A DATEFIELD IN DJANGO – STOCK MANAGEMENT SYSTEM

Spread the love

Adding Data: If you want to automatically add a date and time in a field only when adding an object for the first time in a Django App, set the auto_now_add=True.

...
datefield = models.DateTimeField(auto_now_add=True, auto_now=False)
...

Updating Data: If you want to add date and time only when the object is being edited, set auto_now=True

...
datefield = models.DateTimeField(auto_now_add=False, auto_now=True)
...

If you want to add date and time while adding or updating data, set both auto_now_add=True and auto_now=True

...
datefield = models.DateTimeField(auto_now_add=True, auto_now=True)
...

If you want to only user’s of the application to choose the date and time, then set both auto_not_add=False and auto_now=False

...
datefield = models.DateTimeField(auto_now_add=False, auto_now=False)
...

Spread the love

About the author

arbadjie

Hi, I'm Abdourahman Badjie, an aspiring developer with obsession for anything coding and networking. This blog is dedicated to helping people learn to develop web applications using django framework.

View all posts

18 Comments

Leave a Reply to arbadjie Cancel reply

Your email address will not be published. Required fields are marked *