管理员
class PersonInlines(admin.StackedInline):
model = Person
extra = 0
class CompanyAdmin(admin.ModelAdmin):
inlines = (PersonInlines,)
fields = ('name', )
admin.site.register(Company, CompanyAdmin)
class PersonAdmin(admin.ModelAdmin):
def image_thumbnail(self, obj):
if obj.photo:
image = FileObject(obj.photo.name)
if image.filetype == "Image":
return '<img src="%s" />' % image.version_generate(ADMIN_THUMBNAIL).url
else:
return ""
image_thumbnail.allow_tags = True
image_thumbnail.short_description = "Thumbnail"
fields = ('first_name', 'last_name', 'photo')
list_display = ('first_name', 'last_name', 'photo',)
admin.site.register(Person, PersonAdmin)
模型
def upload_to(instance, filename):
filename = time.strftime('%Y%m%d%H%M%S')
ym = time.strftime('%Y%m')
return 'uploads/%s/%s.jpg' % (ym,filename)
class Person(models.Model):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
photo = models.ImageField(u"Image (Upload)",upload_to=upload_to)
company = models.ForeignKey('Company')
def image(self):
if self.photo:
return FileObject(self.photo.name)
return None
class Company(models.Model):
name = models.CharField(max_length=30)
def __unicode__(self):
return self.name
错误 'PersonAdmin.fields'指的是表单中缺少的字段'image_thumbnail'。
如何使用Django的InlineModelAdmin TabularInline或TabularInline显示缩略图
像这样答案 0 :(得分:0)
您必须引用image_thumbnail
而不是photo
。
fields = ('first_name', 'last_name', 'image_thumbnail')
虽然您可以考虑使用此mixin-approach。