Django: how to change the choices of AdminTimeWidget

后端 未结 6 1799
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-08 12:29

The AdminTimeWidget rendered in admin for a DateTimeField displays an icon of a clock and when you click you have the choice between: \"Now Midnigh

6条回答
  •  南笙
    南笙 (楼主)
    2020-12-08 12:45

    Chris has a great answer. As an alternative you could do this using just javascript. Place the following javascript on the pages where you want the different time options.

    DateTimeShortcuts.overrideTimeOptions = function () {
        // Find the first time element
        timeElement = django.jQuery("ul.timelist li").eq(0).clone();
        originalHref = timeElement.find('a').attr('href');
    
        // remove all existing time elements
        django.jQuery("ul.timelist li").remove();
    
        // add new time elements representing those you want
        var i=0;
        for (i=0;i<=23;i++) {
            // use a regular expression to update the link
            newHref = originalHref.replace(/Date\([^\)]*\)/g, "Date(1970,1,1," + i + ",0,0,0)");
            // update the text for the element
            timeElement.find('a').attr('href', newHref).text(i+"h");
            // Add the new element into the document
            django.jQuery("ul.timelist").append(timeElement.clone());
        }
    }
    
    addEvent(window, 'load', DateTimeShortcuts.overrideTimeOptions);
    

提交回复
热议问题