在 InlineFormAdmin 表单中使用 ImageUploadField
Using ImageUploadField in an InlineFormAdmin form
如何在 Flask Admin 中将模型中的 属性 渲染为 内联表单 中的图像(上传)字段?我尝试了下面的代码 - 它与 examples of flask-admin. I prefer to use ImageUploadField since it uses PIL to take care of generating thumbnails of images. The alternate approach is to use 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')
Flask-Admin 存在错误。我提出了一个 issue 并提交了一个已合并到 master 的 PR。 v1.3.0 之后的任何版本都应该有此修复。
如何在 Flask Admin 中将模型中的 属性 渲染为 内联表单 中的图像(上传)字段?我尝试了下面的代码 - 它与 examples of flask-admin. I prefer to use ImageUploadField since it uses PIL to take care of generating thumbnails of images. The alternate approach is to use 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')
Flask-Admin 存在错误。我提出了一个 issue 并提交了一个已合并到 master 的 PR。 v1.3.0 之后的任何版本都应该有此修复。