shalama
shalama

Reputation: 1335

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)]

Upvotes: 0

Views: 554

Answers (1)

user15208009
user15208009

Reputation:

Try calling it without the @staticmethod

its just how flask-admin call it column_formatters

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))
    }

Upvotes: 0

Related Questions