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.
So my question is: Is there a way to add a custom property to the column list in a Flask-Admin view?
I am sure that it will work just fine if you would use string:
column_list = ('org_length', 'length')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With