3๐
This will solve the particular problem that is not possible
with DATETIME_FORMAT (as it is ignored in the current Django
implementations despite the documentation), is dirty too and
is similar to ayazโs answer (less global โ will only affect
the admin site list view):
Right after the line
(date_format, datetime_format,time_format) = get_date_formats()
in file (Django is usually in folder Lib/site-packages in
the Python installation)
django/contrib/admin/templatetags/admin_list.py
overwrite the value of datetime_format (for a
models.DateTimeField in the model):
datetime_format = โY-m-d H:i:sOโ
And for date-only fields:
date_format = โY-m-dโ
Restart of the web-server (e.g. development server) or
logging out of the admin interface is NOT necessary for
this change to take effect. A simple refresh in the web-browser
is all what is required.
15๐
With:
USE_L10N = False
DATE_TIME
takes effect, since the localization of l10n overrides DATETIME_FORMAT
and DATE_FORMAT
as documented at: https://docs.djangoproject.com/en/1.9/ref/settings/#date-format
- 'CheckoutView' object has no attribute 'object'
- How to print pretty JSON on a html page from a django template?
- Using Django's m2m_changed to modify what is being saved pre_add
- Django: Faking a field in the admin interface?
7๐
As Ciro Santilli told, localization format overrides DATETIME_FORMAT
in settings when USE_L10N = True
. But you can still override DATETIME_FORMAT
and other date/time formats by creating custom format files as described in Django documentation.
See detailed answer here.
You can override
DATE_FORMAT
,DATETIME_FORMAT
,TIME_FORMAT
and other date/time formats whenUSE_L10N = True
by creating custom format files as described in Django documentation.In summary:
- Set
FORMAT_MODULE_PATH = 'yourproject.formats'
insettings.py
- Create directory structure
yourproject/formats/en
(replacingen
with the corresponding ISO 639-1 locale code if you are using other locale than English) and add__init__.py
files to all directories to make it a valid Python module- Add
formats.py
to the leaf directory, containing the format definitions you want to override, e.g.DATE_FORMAT = 'j. F Y'
.Example from an actual project here.
- How do you access query params in a Django Rest Framework 3.0 serializer?
- Django database synchronization for an offline usage
2๐
The two setting directives should be defined in settings.py
. Could you ensure that the same settings.py
that you are editing is being read when you start the development server?
You could always drop to the Python interactive shell by running python manage.py shell
, and run these commands to ensure whether the date/time format values are getting through fine:
from django.conf import settings
settings.DATE_FORMAT
settings.DATETIME_FORMAT
Ok, I forgot to look it up, but ticket #2203 deals with this. Unfortunately, the ticket remains in pending state.
I remember that for a project that used a certain trunk revision of the 0.97 branch of Django, I worked around that by overwriting the date_format
and datetime_format
values in the get_date_formats()
function inside django/utils/translation/trans_real.py
. It was dirty, but I had already been using a custom Django of sorts for that project, so didnโt see anything going wrong in hacking it trifle more.
- A good way to encrypt database fields?
- Django โ how to filter out GET static and media messages with logging?
- Django Building a queryset with Q objects
0๐
According to my experiments with Django 4.2.1, DATETIME_FORMAT for DateTimeField() works only in Django Templates. In addition, DATE_FORMAT for DateField() and TIME_FORMAT for TimeField() work in Django Templates. *You can see my question explaining it.
And, DATE_INPUT_FORMATS for DateTimeField()
and DateField()
and TIME_INPUT_FORMATS for DateTimeField()
and TimeField()
work in Django Admin. *DATETIME_INPUT_FORMATS doesnโt work to any parts of a Django project and you can see my question explaining it.
*You need to set USE_L10N False
in settings.py
to make these settings above work because USE_L10N
is prior to them.
- TimeField format in Django template
- Create a Session in Django
- Django GROUP BY field value
- Changing password in Django Admin