在InlineFormAdmin表单中使用ImageUploadField

2024-09-28 05:22:23 发布

您现在位置:Python中文网/ 问答频道 /正文

如何使模型中的属性呈现为Flask Admin中内联表单中的图像(上载)字段?我尝试了下面的代码-这是在flask admin的examples中可以找到的稍微的变化。我更喜欢使用ImageUploadField,因为它使用PIL来生成图像的缩略图。另一种方法是使用WTF directly,这很好用。在

# Create models
# Location to LocationImage is one-to-many
class Location(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.Unicode(64))

class LocationImage(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    alt = db.Column(db.Unicode(128))
    path = db.Column(db.String(64))

    location_id = db.Column(db.Integer, db.ForeignKey(Location.id))
    location = db.relation(Location, backref='images')

class InlineModelForm(InlineFormAdmin):
    form_label = 'Image'

    def __init__(self):
        return super(InlineModelForm, self).__init__(LocationImage)

    #This property doesn't seem to have any effect in an inline form
    form_extra_fields = {
        'path': ImageUploadField('Image', base_path=base_path)
    }

# Administrative class
class LocationAdmin(ModelView):
    inline_models = (InlineModelForm(),)

    def __init__(self):
        super(LocationAdmin, self).__init__(Location, db.session, name='Locations')

Tags: topath图像selfformiddbinit
1条回答
网友
1楼 · 发布于 2024-09-28 05:22:23

烧瓶管理员有个窃听器。我提出了一个issue并提交了一个已合并到master的PR。v1.3.0之后的任何版本都应该有这个修复。在

相关问题 更多 >

    热门问题