Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask-Admin: Add custom property to column_list

I need to add a custom property of a SQLAlchemy model class to my Flask-Admin view. It does some calculation which I want to display in my column list.

class Withdrawal(db.Model):   
    __tablename__ = 'withdrawals'
    id = db.Column(db.Integer, primary_key=True)
    fabric_id = db.Column(db.Integer, db.ForeignKey('fabrics.id'))
    fabric  = db.relationship('Fabric', backref='withdrawals')
    length = db.Column(db.Float)

class Fabric(db.Model):
    __tablename__ = 'fabrics'
    name = db.Column(db.String(256))
    id = db.Column(db.Integer, primary_key=True)
    org_length = db.Column(db.Float)

    @property
    def length(self):
        return self.org_length - sum(x.length for x in self.withdrawals)

I added the Fabric.length property to my views column_list but won't get to see a value in the new column.

class FabricModelView(ModelView):   
    column_list = ('org_length', Fabric.length)

The result should be a column with the calculated length but actually nothing is shown in the added column.

Screenshot

So my question is: Is there a way to add a custom property to the column list in a Flask-Admin view?

like image 682
MrLeeh Avatar asked Dec 23 '22 21:12

MrLeeh


1 Answers

I am sure that it will work just fine if you would use string:

column_list = ('org_length', 'length')
like image 171
ponyeatsrainbow Avatar answered Dec 28 '22 10:12

ponyeatsrainbow