Let’s say I have a model with a field based on the ImageField class.
class Foo(models.Model):
imagefile = models.ImageField('File', upload_to='foo/%Y/%m%/%d/')
Django adds – after first upload – an input tag of type file to let me change it and a link to the image to view it.
I want this original field specific (HTML) code as is (and by no means create it myself manually) but also add other HTML/JS code, say to include a thumbnail-preview or add some AJAX-stuff. I can image a few other use cases for other fields, too.
What’s the correct (say: easy/unobtrusive) way to implement something like that?
You need to write a custom widget. Look at
django.forms.widgetsfor the code of the existingFileInputwidget, which you can subclass and override therendermethod where necessary. You’ll then just need to assign that widget for your file field in your admin form.