views:

447

answers:

1

Hello (please excuse me for my bad english ;) ),

Imagine the classes bellow:

models.py

from django import models

class MyModel(models.Model):
    content_type = models.ForeignKey(ContentType, verbose_name=_('content type'))
    object_id = models.PositiveIntegerField(_('object id'))
    content_object = generic.GenericForeignKey('content_type', 'object_id')
    published_at = models.DateTimeField()

forms.py

from django import forms

class MyModelForm(forms.ModelForm):
    published_at = forms.DateTimeField(required=False, widget=DateTimeInput)

admin.py

from django.contrib import admin
form django.contrib.contenttypes import generic

class MyModelInline(generic.GenericStackedInline):
    model = MyModel
    form = MyModelForm

class MyModelAdmin(admin.ModelAdmin):
    inlines = [MyModelInline]

Problem: the <script> tags for javascript from the DateTimeInput widget don't appear in the admin site (adding a new MyModel object). i.e. these two lines :

<script type="text/javascript" src="/admin/media/js/calendar.js"></script>
<script type="text/javascript" src="/admin/media/js/admin/DateTimeShortcuts.js"></script>

Please, do you have any idea to fix it ?

Thank you very much and have a good day :)

+3  A: 

The standard DateTimeWidget doesn't include any javascript. The widget used in the admin is a different one - django.contrib.admin.widgets.AdminSplitDateTime - and this includes the javascript.

Daniel Roseman
Specific code to implement this solution at http://stackoverflow.com/questions/38601/using-django-time-date-widgets-in-custom-form/38916#38916
John Paulett