'Detect timezone in django admin
I'm working in a project with a Django backend (only backend and a default admin portal, no site) where the admin portal is used by people both in Europe and US.
Because of this, it's important that the datetimes in the admin portal are displayed in the local timezone of whomever is using it.
For example, in some models I display the creation date of instances. I need those dates to be displayed in the timezone of whomever accesses the admin portal.
I've searched for solutions to achieve this (such as suggested in the docs, but also this package) but all the solutions I've found seem to be made for detecting the timezone of end-users accessing a custom website, not the default admin portal.
I'm using Django 2.2 and Python 3.8.
Solution 1:[1]
One of the methods to achieve this is by using a custom field in Django ModelAdmin.
References for:
from django.contrib import admin
from django.db import models
class AnItem(models.Model):
title = models.CharField(max_length=150)
creation_date = models.DateField()
@admin.display(description='Local Time')
def local_time_of_creation(self):
local_time = write_logic_to_get_the_local_time_here
return local_time
class AnItem(admin.ModelAdmin):
list_display = ('name', 'local_time_of_creation')
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | rs_punia |
