0

I have a datefield named pub_date in my django project. I want to use jQuery datepicker instead of default django datepicker in django admin. But I want to do it without using django forms. Is it possible to do that? I'm giving my code below.

models.py

   import datetime

   from django.db import models

   class Poll(models.Model):
       question = models.CharField(max_length=200)
       date     = models.DateField('Registration Date')
       pub_date = models.DateTimeField('date published')

       def __unicode__(self):
           return self.question

       def was_published_today(self):
           return self.pub_date.date() == datetime.date.today()
       was_published_today.short_description = 'Published today?'

   class Choice(models.Model):
       poll   = models.ForeignKey(Poll)
       choice = models.CharField(max_length=200)
       votes  = models.IntegerField()

       def __unicode__(self):
           return self.choice

admin.py

    from django.contrib import admin

    from poject.app.models import Poll, Choice

    class ChoiceInline(admin.TabularInline):
        model = Choice
        extra = 3

    class PollAdmin(admin.ModelAdmin):
        fieldsets = [
            (None,               {'fields': ['question']}),
            ('Date information', {'fields': ['pub_date'], 'classes': ['collapse']}),
        ]
        inlines        = [ChoiceInline]
        list_display   = ('question', 'pub_date', 'was_published_today')
        list_filter    = ['pub_date']
        search_fields  = ['question']
        date_hierarchy = 'pub_date'

    admin.site.register(Poll, PollAdmin)
    admin.site.register(Choice)

Is it possible to use jquery datepicker in the DateField and DateTimeField defined in the above models without using django forms?

arulmr
  • 8,620
  • 9
  • 54
  • 69

2 Answers2

1

If you don't want to create a form widget you can override the datepicker by overriding your admin change_form.html template.

dan-klasson
  • 13,734
  • 14
  • 63
  • 101
  • I have displayed jquery datepicker in django admin using this method. But I couldn't hide default django datepicker shortcuts. How to hide them or how to replace default django datepicker with jquery datepicker. – arulmr Sep 01 '12 at 06:53
0

Check out my question Change the default widget for date fields in Django admin about how to do this for all DateField instances.

If you don't want to touch forms, provide your own js/calendar.js and js/admin/DateTimeShortcuts.js - if you want to do this only for a particular model you'll need to override its change_form.html template and replace the default javascript files just there. What the javascript does is look for date(time) fields and insert the datepicker - yours would do the same, just with a different datepicker.

Community
  • 1
  • 1
Danny W. Adair
  • 12,498
  • 4
  • 43
  • 49
  • Hi. Thanks for your reply. I tried the methods given in yr question. But I couldn't get them working. I'm a beginner in django and I'm not so clear about overriding default datepicker js. So if possible, can you please explain a little more detail to me with samples. Thanks again for your help. – arulmr Mar 19 '12 at 14:35
  • The other answer (a subset) has become more useful? Look at https://github.com/django/django/blob/master/django/contrib/admin/static/admin/js/admin/DateTimeShortcuts.js - admin (widgets) use DateTimeShortcuts.init() to append a "calendar icon" after every input field with class "vDateField". The actual calendar is drawn with https://github.com/django/django/blob/master/django/contrib/admin/static/admin/js/calendar.js - in your static folder just provide admin/js/calendar.js and admin/js/admin/DateTimeShortcuts.js - then they will be used instead. Then mimick other DateTimeShortcuts functions. – Danny W. Adair Jul 02 '12 at 08:44