基本上,我想要實現的是在添加或編輯界面中選擇文件後,顯示圖像縮略圖的媒體表單。我嘗試使用此代碼,但它返回此錯誤。如何在django admin字段中添加自定義html?
Unknown field(s) (admin_image) specified for ImageMedia.
Check fields/fieldsets/exclude attributes of class ImageMediaAdmin.
這是模型類:
class ImageMedia(models.Model):
caption = models.CharField(max_length=64)
picture = models.ImageField(upload_to='image_media', blank=True)
def admin_image(self):
return '<img src="%s" height="300px"/>' % self.picture
admin_image.allow_tags = True
def __str__(self):
return self.caption
這是管理類
class ImageMediaAdmin(admin.ModelAdmin):
list_display = ('caption', 'picture', 'admin_image')
search_fields = ('caption', 'picture')
fields = ('caption', 'picture', 'admin_image')
admin.site.register(ImageMedia, ImageMediaAdmin)
您需要將它從'fields'中移除,因爲它不是一個字段而是一個函數;這將擺脫眼前的問題。 – 2014-10-06 04:08:50