Django admin — Display time in the current timezone

I had a Django project where a cronjob will run at certain intervals and add an entry in the database if the task is executed successfully. My project used UTC as the default timezone.

When I look at the execution time of the tasks in the Django-admin site all the execution times were in UTC. Although it is not a big deal, it bothered me a little. I wanted to display those times in my current timezone. I searched for a lot of ways to do this but all ended in vain.

Most of the solutions wanted me to modify my Django-admin static files. Being a backend developer I did not want to do that. Finally, I found a solution.

The solution is,

Modify the time data in the file before returning it to the Django-admin site.

This is how my table looked before.

This is how it looked after making the change.

I have added the following piece of code to my of the app.

My CronTask model had two fields name and time. Name is the task that is executed and time is the execution time.

Instead of displaying the name and time directly on the Django-admin page, I have created a new field called get_ist. This field will convert the time to my current timezone. Since my timezone is IST I am simply adding 5:30 hrs to the execution time.

The short description attribute will rename the column name to the value provided.

Note: This will not alter the time in the database.


Hope this article was helpful. Happy coding!

Leave a Reply

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