How to format DateTimeField in Admin according to localtime and timezone ?
My settings.py:
TIME_ZONE = 'Europe/Bratislava'
LANGUAGE_CODE = 'en-us'
USE_I18N = True
USE_L10N = True
USE_TZ = True
pytz package is installed.
model:
class Material(models.Model):
category = models.ForeignKey(Category, null=True, blank=True)
code = models.CharField(max_length=10)
description = models.CharField(max_length=30, blank=True, null=True)
modified = models.DateTimeField(auto_now=True)
created = models.DateTimeField(auto_now_add=True)
Also tried some date formatting in settings, none of this changed the way datetime object is converted to string in admin list display:
DATETIME_FORMAT = 'd N Y'
DATE_FORMAT = 'd N Y'
In database datetime is stored correctly, "2012-11-20 08:57:15.901341+01". But when displayed in admin, it is always in UTC.
I can prepare methods in ModelAdmin to handle format, but that is not really DRY as I'd like to my admin classes look like:
from django.utils.timezone import localtime
class MaterialAdmin(admin.ModelAdmin):
list_display = ('code', 'modified_local', 'created')
def modified_local(self, row):
return localtime(row.modified)
modified_local.admin_order_field = 'modified'
modified_local.short_description = 'Modified'
The answer to you question is proper configuration of
settings
andformats
in Django project. Structure of example project:You can define it for multiple languages just by providing directory with appropriate name and formats.py inside. The example content of
formats.py
where all the MAGIC happens can look as follows:Please notice two links in the comments, which will guide you to lists of proper configurations, which ARE DIFFERENT for DIFFERENT parts!
In your
settings.py
just add:[GITHUB] Here is a full working example: https://github.com/andilab/demo_time_set