简体   繁体   中英

Flask-Admin column_formatters not called

I'm using Flask-Admin and would like to add an extra field to my inline model view. Purpose is to display an Image from the filesystem.

Issue is that my column formatter is never called and the image never gets loaded. Any suggestions would be much appreciated.

class ImageView(ModelView):
    form_columns = ['id', 'mime_type', 'path']

    @staticmethod
    def _user_formatter(view, context, model, name):
        logging.warning("NOT CALLED")
        return Markup('<img src="%s">' % form.thumbgen_filename(1))

    column_formatters = dict(path=lambda v, c, m, p: ImageView._user_formatter(v, c, m, p))

    form_extra_fields = {
        'path': form.ImageUploadField('Image', base_path='/app/uploads', thumbnail_size=(400, 400, True))
    }

class VehicleModelView(ModelView):
    inline_models = [ImageView(DbModel, db.session)]

Try calling it without the @staticmethod

class ImageView(ModelView):
    form_columns = ['id', 'mime_type', 'path']

    def _user_formatter(view, context, model, name):
        logging.warning("NOT CALLED")
        return Markup('<img src="%s">' % form.thumbgen_filename(1))

    column_formatters = dict(path=_user_formatter)

    form_extra_fields = {
        'path': form.ImageUploadField('Image', base_path='/app/uploads', thumbnail_size=(400, 400, True))
    }

here is an example given by Flask-Admin

class ImageView(sqla.ModelView):
    def _list_thumbnail(view, context, model, name):
        if not model.path:
            return ''

        return Markup('<img src="%s">' % url_for('static',
                                                 filename=form.thumbgen_filename(model.path)))

    column_formatters = {
        'path': _list_thumbnail
    }

    # Alternative way to contribute field is to override it completely.
    # In this case, Flask-Admin won't attempt to merge various parameters for the field.
    form_extra_fields = {
        'path': form.ImageUploadField('Image',
                                      base_path=file_path,
                                      thumbnail_size=(100, 100, True))
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM