views:

32

answers:

1

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?

+2  A: 

You need to write a custom widget. Look at django.forms.widgets for the code of the existing FileInput widget, which you can subclass and override the render method where necessary. You'll then just need to assign that widget for your file field in your admin form.

Daniel Roseman
Great, good that I know too :) Thanks!
cpf
Hi Daniel, can you give a simple example of how to do that? I'm not getting it to work. I tried to point my FooAdmin class via "form=" to a special forms.ModelForm class with a inner Meta class referencing to the main model (as explained somewhere in the documentation). But I'm stuck with the rest.
initall