Feb 19

Django time widget with custom time shortcuts

How to customize Django admin time widget to set desired time shortcuts.

This small javascript snippet replaces standard django clock shortcuts Midnight, Noon, Now & 6am with various time increments .

$(window).load(function() {
  $('.timelist').each(function(num, el) {
    time_format = get_format('TIME_INPUT_FORMATS')[0];
    for (i=8; i<20; i++) {
      var time = new Date(1970,1,1,i,0,0);
      lnk = "javascript:DateTimeShortcuts.handleClockQuicklink(" + num + 
                  ", '" + time.strftime(time_format) + "');"
      $(el).append('<li><a href="'lnk'">' + time.strftime('%H:%M') + '</a></li>');

This snippet use JQuery which is available with Django 1.2. (dev at time of writing)
Also, javascript should be loaded after DateTimeShortcuts.js but unfortunately I didn’t find a better way to achive this but to add in modified base admin site templates/admin/base_site.html. If you have any suggestion for improvement, let me know in comments.