我在Github上的 FAB 问题网站上找到了解决方案。在models.py中,你可以定义一个方法,然后使用views.py中的方法。然后资源列表页面会将方法视为附加列。该解决方案有一个缺点,您必须在模型方法中编写 HTML。
这是我的代码。
模型.py
class Device(Model):
id = Column(Integer, primary_key = True)
snr = Column(String(256), unique = True)
name = Column(String(128))
addr = Column(String(256))
latitude = Column(Float)
longitude = Column(Float)
status = Column(Enum('init','normal','transfer','suspend'), default = 'init')
owner_id = Column(Integer, ForeignKey('account.id'))
owner = relationship("Account")
def __repr__(self):
return self.name
def get_gauge_url(self):
btn = "<a href=\"/gaugeview/{}\" class=\"btn btn-social-icon\"><i class=\"fa fa-dashboard\"></a>".format(self.id)
return btn
视图.py
class DeviceView(ModelView):
datamodel = SQLAInterface(Device)
related_views = [PermitView, EventView]
label_columns = {'snr':'SNR',
'owner_id':'Owner',
'get_gauge_url':'Gauge'}
list_columns = ['name','snr','addr','owner','get_gauge_url']
edit_columns = ['name','snr','owner','addr','latitude','longitude','status',]
add_columns = edit_columns
show_fieldsets = [
('Summary',
{'fields':['name','snr','owner']}
),
('Device Info',
{'fields':['addr','latitude','longitude','status'],'expanded':True}
),
]