Django: how to change the choices of AdminTimeWidget

后端 未结 6 1798
佛祖请我去吃肉
佛祖请我去吃肉 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:48

    I tried using this method and found the above javascript didn't work when multiple datetime's were present on the form.

    here is what I did.

    In my ModelAdmin section i added:

    class Media:
        js = ('js/clock_time_selections.js',)
    

    then in the js file:

    $('document').ready(function () {
        DateTimeShortcuts.overrideTimeOptions = function () {
            var clockCount = 0;
            console.log('ready');
            $('ul.timelist').each(function () {
                var $this = $(this);
                var originalHref = $this.find('a').attr('href');
                console.log(originalHref);
                $this.find('li').remove();
                for (i=8; i <= 20; i++) {
                    var newLink = '
  • ' + i + ':00h
  • '; $this.append(newLink); } //console.log($this.html()); clockCount++; }); }; addEvent(window, 'load', DateTimeShortcuts.overrideTimeOptions); });

    Note: i had to put inside a document.ready because i found that i couldn't control where the script was included in the page (seems to have be loaded before the default calendar js files).

提交回复
热议问题