This is easy for non-inlines. Just override the following in the your admin.py AdminOptions:
这对于非内联很容易。只需覆盖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 个解决方案
#1
8
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.
它的工作方式完全相同。 TabularInline和StackedInline类也有一个formfield_for_dbfield方法,你可以在子类中以相同的方式覆盖它。
#2
6
Since Django 1.1, formfield_overrides is also working
从Django 1.1开始,formfield_overrides也在工作
formfield_overrides = {
models.ImageField: {'widget': AdminImageWidget},
}
#3
1
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)
#1
8
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.
它的工作方式完全相同。 TabularInline和StackedInline类也有一个formfield_for_dbfield方法,你可以在子类中以相同的方式覆盖它。
#2
6
Since Django 1.1, formfield_overrides is also working
从Django 1.1开始,formfield_overrides也在工作
formfield_overrides = {
models.ImageField: {'widget': AdminImageWidget},
}
#3
1
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)