我有一个Entry包含简单属性的model():
@property
def image(self):
return str(self.id)+"_"+self.round.season.name+"_"+self.round.theme+"_"+self.person.name
Run Code Online (Sandbox Code Playgroud)
我用它来构建磁盘上特定图像文件的名称.所以我知道会有一个图像/path/to/images/(model.image()).jpg
我可以在管理页面上的TabularInline布局中显示原始图像属性本身,方法是将其添加到readonly_fields集合中,但是如何获取一个包含模型属性的自定义html的列?
例如
<img src="/images/{{model.image}}.jpg" />
Run Code Online (Sandbox Code Playgroud)
gla*_*xly 19
你可以做的是建立在你的方法TabularInline子类返回你想要的HTML,然后在地方使用该方法的名称image中ImageInline.fields:
from django.utils.safestring import mark_safe
class ImageInline(admin.TabularInline):
...
fields = (..., 'render_image')
def render_image(self, obj):
return mark_safe("""<img src="/images/%s.jpg" />""" % obj.image)
Run Code Online (Sandbox Code Playgroud)
lec*_*hup 18
根据当前的Django 1.2+,我得到了错误"Form没有像render_image这样的字段".解决方案很简单,将render_image函数放入model.Admin不在你的内联表单中,第二件事是你的内联表单中的字段和readonly_fields设置......所以这里你有我最终得到的结果:
class OfferPropertyInline(admin.TabularInline):
model = OfferProperty
fields=('property_value',)
readonly_fields = ('property_value',)
class OfferAdmin(admin.ModelAdmin):
inlines = [
OfferPropertyInline
]
def property_value(self,obj):
return obj.get_value()
admin.site.register(Offer, OfferAdmin)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
24953 次 |
| 最近记录: |