Flask-Admin: Add custom property to column_list
Asked Answered
A

1

5

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?

Arkansas answered 17/1, 2017 at 18:16 Comment(0)
B
11

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

column_list = ('org_length', 'length')
Backdrop answered 17/1, 2017 at 18:52 Comment(1)
Honestly, this works. I guess I missed the wood for the trees here. ThxArkansas

© 2022 - 2024 — McMap. All rights reserved.