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'
DateTimeField in Django Forms is a date field, for taking input of date and time from user. The default widget for this input is DateTimeInput. It Normalizes to: A Python datetime. datetime object.
If you want to be able to modify this field, set the following instead of auto_now_add=True : For DateField : default=date.today - from datetime.date.today() For DateTimeField : default=timezone.now - from django.utils.timezone.now()
To enable it, set USE_TZ = True in your settings file. In Django 5.0, time zone support will be enabled by default. Time zone support uses zoneinfo , which is part of the Python standard library from Python 3.9.
First, open the views.py file of your Django application and import the datetime module. Next, use the datetime. now() method to get the current date and time value. Now, we can either assign this method to a variable or we can directly use this method wherever we required the datetime value.
The answer to you question is proper configuration of settings
and formats
in Django project. Structure of example project:
.
|-- README.md
|-- demo.db
|-- demo_time_set
| |-- __init__.py
| |-- demo.db
| |-- formats
| | |-- __init__.py
| | `-- en
| | |-- __init__.py
| | `-- formats.py
| |-- settings.py
| |-- urls.py
| `-- wsgi.py
|-- manage.py
|-- requirments.txt
`-- some_app
|-- __init__.py
|-- admin.py
`-- models.py
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:
# HERE FORMATING AS shown in:
# LIST: https://docs.djangoproject.com/en/dev/ref/templates/builtins/#date
DATE_FORMAT = 'd-m-Y'
TIME_FORMAT = 'H:i'
DATETIME_FORMAT = 'd-m-Y H:i'
YEAR_MONTH_FORMAT = 'F Y'
MONTH_DAY_FORMAT = 'F j'
SHORT_DATE_FORMAT = 'm/d/Y'
SHORT_DATETIME_FORMAT = 'm/d/Y P'
FIRST_DAY_OF_WEEK = 1
# BUT here use the Python strftime format syntax,
# LIST: http://docs.python.org/library/datetime.html#strftime-strptime-behavior
DATE_INPUT_FORMATS = (
'%d-%m-%Y', # '21-03-2014'
)
TIME_INPUT_FORMATS = (
'%H:%M:%S', # '17:59:59'
'%H:%M', # '17:59'
)
DATETIME_INPUT_FORMATS = (
'%d-%m-%Y %H:%M', # '21-03-2014 17:59'
)
DECIMAL_SEPARATOR = u'.'
THOUSAND_SEPARATOR = u','
NUMBER_GROUPING = 3
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:
FORMAT_MODULE_PATH = 'demo_time_set.formats'
[GITHUB] Here is a full working example: https://github.com/andilab/demo_time_set
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With