Login

Creating custom time entries in Django Date widget

Author:
adiq
Posted:
May 29, 2014
Language:
Python
Version:
Not specified
Score:
0 (after 0 ratings)

jQuery code for making custom list on Admin page in DateTime widget. Create new js file in your static folder with this code.

To use add custom js to Admin page like this:

class NiceAdmin(admin.ModelAdmin):

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

This code will change all DateTime widgets on selected page.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
if (!$) {
    $ = django.jQuery;
}

function overrideTimeOptions() {

    $("ul.timelist").each(function(){

        entries = $(this).children("li");

        baseEntry = entries.first();
        baseHref = baseEntry.find("a").attr("href");

        entries.remove();

        newEntryHref = baseHref.replace(/Date\([^\)]*\)/g, "Date(1970,1,1,6,0,0,0)");
        newEntry = '<li><a href="'+ newEntryHref +'">06:00:00</a></li>';
        $(this).append(newEntry);

    });


}

setTimeout(function(){overrideTimeOptions()},500);

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 6 months ago

Comments

mykl (on August 21, 2015):

Nice code, works in 1.7 Question, I want multiple dates to appear in the drop-down which will be retrieved via a MySQL query.

How can I pass the array of times here?

#

Please login first before commenting.