views:

1091

answers:

3

This is easy for non-inlines. Just override the following in the your admin.py AdminOptions:

def formfield_for_dbfield(self, db_field, **kwargs):
    if db_field.name == 'photo':
        kwargs['widget'] = AdminImageWidget()
        return db_field.formfield(**kwargs)
    return super(NewsOptions,self).formfield_for_dbfield(db_field,**kwargs)

I can't work out how to adapt this to work for inlines.

+3  A: 

It works exactly the same way. The TabularInline and StackedInline classes also have a formfield_for_dbfield method, and you override it the same way in your subclass.

Carl Meyer
Doh! I was over-complicating things and fiddling around with InlineFormsetFactory and the like!
andybak
+1  A: 

Edit: Nevermind. This was just a stupid error on my part.

Could you possibly provide a small snippet of this working in inlines? When I try the code below I'm just getting some weird keyerror.

class PictureInline(admin.StackedInline):
    model = Picture_Gallery
    extra = 3
    def formfield_for_dbfield(self, db_field, **kwargs):
     if db_field.name == 'name':
      kwargs['widget'] = MyWidget()
      return super(PictureInline,self).formfield_for_dbfield(db_field,**kwargs)
sjtm
+1  A: 

Since Django 1.1, formfield_overrides is also working

formfield_overrides = {
    models.ImageField: {'widget': AdminImageWidget},
}
luc