Guinslym:: My Blog

How to add Timestamps in your Django model

If you want to add timestamps here a three options that you can take.


First let's create a new project and a new app.

 -> %  django-admin startproject blogproject && cd blogproject
 -> %  python manage.py startapp blog

Simplicity

let's add the new app blog to our installed apps

Now we can start creating our model. We will have four fields: title, description, updated, created

Being smarter

If you planned to use these fields often in other Models. i.e. Comments, Like, ShoppingCart etc. It would be smarter keep things DRY

Being smarter (suite)

There is an awesome package called django-model-utils. If you planned to use more than one of the functionnalities offers in this package i.e. StatusField, FieldTracker you might as well use this package to add the fields created and updated

     -> %  pip install django-model-utils
    

After we will import it in your models.py

Guinsly Mondésir Ottawa

Web developer - Pythonist - Rubyist

doi cli curl HTTP