Home > database >  How can I make the Flask-admin panel use the init method of the model?
How can I make the Flask-admin panel use the init method of the model?

Time:10-31

When I add a new collection through Flask-admin panel I don't get the init method to reduce the image through another function. And when I add a new collection through the Python console everything works. But Flask-admin panel...

Model:

class Collections(db.Model):
    __tablename__ = 'collections'

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String, nullable=False)
    preview_photo = db.Column(db.String, nullable=False)

    def __init__(self, name, preview_photo):
        self.name = name
        self.preview_photo = collections_resize(preview_photo)

    def __repr__(self):
        return f'{self.name}' 

As you can see, in the init method I pass in self.thumb_photo the result of the collections_resize() function. But the method doesn't run through the panel, can you explain why?

View model:

class CollectionsView(ModelView):
    form_columns = ['name', 'preview_photo']

admin.add_view(CollectionsView(Collections, db.session))

CodePudding user response:

I ended up rewriting the view model this way and it worked, but I had to write a separate html for it and it looks really weird, but it will do. If you have any better ideas, suggest them.

class CollectionsView(ModelView):
    form_columns = ['id', 'name', 'preview_photo']

    @expose('/new/', methods=('GET', 'POST'))
    def create_view(self):
        if request.method == 'POST':
            if 'file' not in request.files:
                return redirect(request.url)

            file = request.files['file']

            if file.filename == '':
                return redirect(request.url)

            if file and allowed_file(file.filename):
                filename = file.filename
                file.save(os.path.join('path', filename))
                name = request.form.get('name')
                preview_photo = filename
                q = Collections(name=name, preview_photo=preview_photo)
                db.session.add(q)
                db.session.commit()
                return redirect('/admin/collections')

        return render_template('create_coll.html')

CodePudding user response:

Override create_model.

class CollectionsView(ModelView):
    form_columns = ['name', 'preview_photo']

    def build_new_instance_and_populate_obj(form):
        name = form.get('name')
        preview_photo = form.get('preview_photo')
        return Collections(name=name, preview_photo=preview_photo)

    def create_model(self, form):
        try:
            # model = self.build_new_instance()
            # form.populate_obj(model)
            model = self.build_new_instance_and_populate_obj(form)

            self.session.add(model)
            self._on_model_change(form, model, True)
            self.session.commit()
        except Exception as ex:
            if not self.handle_view_exception(ex):
                flash(gettext('Failed to create record. %(error)s', error=str(ex)), 'error')
                log.exception('Failed to create record.')

            self.session.rollback()

            return False
        else:
            self.after_model_change(form, model, True)

        return model

Reference: flask-admin/flask-admin#1927

  • Related