How can I change the default widget for DateField instances in Django ADMIN?
I am aware of how to do this for a ModelForm – How do you change the default widget for all Django date fields in a ModelForm? – by providing formfield_callback that checks the field type and then provides a form field with the new widget.
However, some of my ModelAdmin’s may already have a custom ModelForm, some haven’t. I don’t want to provide/change a ModelForm wherever there’s a date field (that’s the point).
A custom ModelAdmin which could be used for subclassing would be ok, though. How can a ModelAdmin change the date fields of its form, independent of whether a custom ModelForm has been provided or not?
Maybe a ModelAdmin with get_form() calling its super then setting the form’s formfield_callback? Seems a little convoluted, not sure about the timing, and also then there’s three things to be aware of if any ModelAdmin needs further customization…
P.S.: I’m asking this after https://stackoverflow.com/questions/8484811/jquery-datepicker-in-django-admin was erroneously closed for being an “exact duplicate” of the above-mentioned ModelForm question. I hope the difference is now clear.
Btw my personal motivation is the year navigation of django admin’s default datepicker. Really bad for birthdays… 🙂 yes there’s a three-year old ticket https://code.djangoproject.com/ticket/9388
ModelAdmin can only change the widget of the form it renders, so it would be difficult to override the widgets of a custom form passed to it – at least I can’t think of a clean way to do it.
I can see a few options for you, depending on how/what you want to customize.
Modify the javascript that does the magic for the widget. This seems easy enough if your change can be handled there. Simply provide your own
js/calendar.jsandjs/admin/DateTimeShortcuts.jsthe same way you would override the admin’s templates. This has the added bonus of being upgrade friendly; since you are not modifying the django code base; also the change would be immediate for all forms that use the widget.Customize the built-in django widget to do what you want. This has the benefit of instantly working without any modifications of your existing code, but is not upgrade friendly unless you are careful about patching sources. The source is at
django.contrib.admin.widgetsProvide your own custom widget – but then you are back to the problem of going through the code and manually overriding all widget instances with your custom one.
Use something like grappelli and modify the templates/js to do what you want. Added bonus here is that you’ll get lots of other functionality for free (like the ability to have a dashboard, if you plug it in with django-admin-tools).
django uses jquery extensively in their admin and they have their own jquery namespace (
django.jQuery); so if you plan on doing some custom widgets make sure you use the right namespace to avoid strange problems.