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. codigo alto nivel by MrRocklion 1 month, 1 week ago
  2. Load template from specific app by Krzysiek555 1 month, 4 weeks ago
  3. PostgreSQL JSON subqueries by dolamroth 2 months ago
  4. "Magic Link" Management Command by webology 7 months ago
  5. Closest ORM models to a latitude/longitude point by simonw 7 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.