Opened 17 months ago

Closed 17 months ago

Last modified 17 months ago

#34241 closed New feature (wontfix)

Django admin not showing seconds for list_display nor readonly DateTimeField

Reported by: Petr Dlouhý Owned by: nobody
Component: contrib.admin Version: 4.1
Severity: Normal Keywords:
Cc: Carlton Gibson Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: yes

Description

The default output format for DateTimeField (e.g. created) fields in Django admin is something like Jan. 4, 2023, 4:22 p.m.. When I am debugging some issue and trying to find matching record in logs I am lacking information about second (or even milliseconds).

The only solutions I found is either to change DATETIME_FORMAT systemwise:

from django.conf.locale.en import formats as en_formats
en_formats.DATETIME_FORMAT = "d M. Y H:i:s"

or to change the output for individual fields.

But I would like to see seconds for all DateTime fields in whole admin interface and not to change the format in frontend nor API (which the first solution causes).

Most importantly I feel, that all Django users would benefit from ability to access whole information about DateTime which is stored in the database. So my suggestion is to start outputting DateTime fields in admin interface with tooltip containing whole precise time such as:

<div class="readonly" title="Jan. 4, 2023, 4:22:30.123 p.m.">Jan. 4, 2023, 4:22 p.m.</div>

Attachments (2)

Snímek obrazovky_2023-01-05_10-13-50.png (6.8 KB ) - added by Petr Dlouhý 17 months ago.
Snímek obrazovky_2023-01-05_10-13-37.png (3.1 KB ) - added by Petr Dlouhý 17 months ago.

Download all attachments as: .zip

Change History (11)

by Petr Dlouhý, 17 months ago

by Petr Dlouhý, 17 months ago

in reply to:  description comment:1 by Mariusz Felisiak, 17 months ago

Resolution: wontfix
Status: newclosed

Replying to Petr Dlouhý:

The default output format for DateTimeField (e.g. created) fields in Django admin is something like Jan. 4, 2023, 4:22 p.m.. When I am debugging some issue and trying to find matching record in logs I am lacking information about second (or even milliseconds).

Thanks for the ticket, however I find it unusual to use the admin for debugging, especially so precise that seconds/milliseconds are needed. I'd recommend using the dbshell/shell console to search for specific rows/objects.

The only solutions I found is either to change DATETIME_FORMAT systemwise:

from django.conf.locale.en import formats as en_formats
en_formats.DATETIME_FORMAT = "d M. Y H:i:s"

or to change the output for individual fields.

But I would like to see seconds for all DateTime fields in whole admin interface and not to change the format in frontend nor API (which the first solution causes).

Most importantly I feel, that all Django users would benefit from ability to access whole information about DateTime which is stored in the database. So my suggestion is to start outputting DateTime fields in admin interface with tooltip containing whole precise time such as:

<div class="readonly" title="Jan. 4, 2023, 4:22:30.123 p.m.">Jan. 4, 2023, 4:22 p.m.</div>

IMO, debugging with the admin doesn't justify pushing extra data to the UI that are not needed and not used otherwise. You can start a discussion on DevelopersMailingList if you don't agree.

comment:2 by Petr Dlouhý, 17 months ago

Thanks for the ticket, however I find it unusual to use the admin for debugging, especially so precise that seconds/milliseconds are needed. I'd recommend using the dbshell/shell console to search for specific rows/objects.

@Mariusz Felisiak Thank you for your answer, but I am feeling that it might be a bit out of sync with everyday production reality.
It might not be debugging per se, but if I got report by other admin about some inconsistency in the data in admin interface I really don't want to open shell/dbshell just find at what second the record was created. That is addition substantial workload.

More importantly the seconds might be very important even for many ordinary admin users. There might be several use-cases - for example:

  • The user made two duplicate payments at same minute and want to return one of them - then the seconds might help the admin to determine which to refund and which to keep (or find matching record in payment system).
  • Many other cases where the user needs to find matching record in some other external database/application.
  • Cases where there are several records created at same minute.
  • The table has higher load and there are several records created every minute.
  • The admin user might be experienced enough in working with the data to analyze the problem with data but not a programmer able to use shell/dbshell (and it might not be desired to give him the permissions).

Overall I think that the only reason to hide the seconds from users might be compactness of the UX, but at the same time I feel that there should be some mechanism to either allow users to access the hidden data in whole admin interface or at least make it possible to easily configure the output data format in admin interface without need to change it systemwide.

comment:3 by Mariusz Felisiak, 17 months ago

Cc: Carlton Gibson added

More importantly the seconds might be very important even for many ordinary admin users. There might be several use-cases.

Agreed, but it's not something all users need for all dates.

... or at least make it possible to easily configure the output data format in admin interface without need to change it systemwide.

You can always change format for a single field, e.g

class MyModelAdmin(admin.ModelAdmin):

    @admin.display(description="Created with seconds")
    def custom_created(self, obj):
        return obj.created.strftime("d M. Y H:i:s")

    list_display = (..., "custom_created")

comment:4 by Petr Dlouhý, 17 months ago

Agreed, but it's not something all users need for all dates.

Most of Django features are not NEEDED for ALL users.
When I am digging deeper I see big inconsistency in the fact, that when I am editing the field I must enter the date including seconds but then the time is otupted in changelist_table or as readonly field without them.

You can always change format for a single field, e.g

That is not very practical nor DRY if I want to make it for every DateTime field in admin including external applications.

Maybe there is some way to override readonly field outputs in Django admin I am not aware of or maybe there isn't and creating something like that would help to solve other tickets like: https://code.djangoproject.com/ticket/30577

comment:5 by Mariusz Felisiak, 17 months ago

Most of Django features are not NEEDED for ALL users.

But we don't force users to use them. Your proposition with title would appear for all admin users.

When I am digging deeper I see big inconsistency in the fact, that when I am editing the field I must enter the date including seconds but then the time is otupted in changelist_table or as readonly field without them.

I understand that you don't agree with my opinion :), so please feel-free to start a discussion on the DevelopersMailingList, where you'll reach a wider audience and see what other think, and follow the guidelines with regards to requesting features.

comment:6 by Carlton Gibson, 17 months ago

I'd imagine ModelAdmin.formfield_overrides would serve for editable fields, with a project level base class if wanted throughout.

I don't think it's feasible for read-only fields without progress on #30577 (which lacks even a proof-of-concept).

I agree that second and sub-second precision is not required (or desired) in the vast majority of cases.

comment:7 by Petr Dlouhý, 17 months ago

OK. For anyone concerned I have find solution that is a bit hacky, but is best of what I can think is possible right now:

In some __init__.py file:

from django.contrib.admin import utils
from django.db.models import DateTimeField
from django.utils.html import format_html


_display_for_field = utils.display_for_field


# Modify DateTime field format and display tooltip with full date and time
def display_for_field(value, field, empty_value_display):
    if value and isinstance(field, DateTimeField):
        return format_html(
            '<span title="{}">{}</span>', value, value.strftime("%Y-%m-%d %H:%M:%S %Z")
        )
    return _display_for_field(value, field, empty_value_display)


utils.display_for_field = display_for_field

comment:8 by Claude Paroz, 17 months ago

I *may* see some value in adding the full iso representation of the datetime as a data attribute of the div. It's probably still not directly addressing the use case of this ticket, but at least would provide a way for some clever tools to extend the UI if needed (e.g. some custom JS).

comment:9 by Petr Dlouhý, 17 months ago

@Claude Paroz That would be very nice. It might even be sufficient for the debuging purposes, because using developer tools is much more easier than going into shell/dbshell.
Also custom JS would be very easy option in such case (although I would probably stick with the solution described further up).

Note: See TracTickets for help on using tickets.
Back to Top