Reputation: 11
I have a model with a price column. Using ModelView
, I want the price column to be right aligned with two decimal places and a currency symbol. How do I change the format of the price column?
Upvotes: 0
Views: 1091
Reputation: 11
If you don't want to include decorate-relevant code in model.py. the following can be a good alternative.
class FormattedListWidget(ListWidget):
template = 'widgets/my_list.html'
class MyModelView(ModelView):
datamodel = SQLAInterface(MyTable)
list_widget = FormattedListWidget
list_columns = ['my_dirthday']
formatters_columns={'my_dirthday':lambda x:'<b>'+x.strftime('%Y.%m.%d')+'</b>'}
my_list.html is from ~/flask_appbuilder/templates/appbuilder/general/widgets/list.html
it must be located in your /templates/widgets directory. and just edit my_list.html as following.
[before]
<td>{{ formatter(item[value])}}
[after editing]
<td>{{ formatter(item[value]) |safe }}
Upvotes: 0
Reputation: 1273
upgrade flask-appbuilder to 1.4.4 and use the new @renders decorator to do this:
Custom Model properties can be used on lists. This is usefull for formating values like currencies, time or dates. or for custom HTML. This is very simple to do, first define your custom property on your Model and use the @renders decorator to tell the framework to map you class method with a certain Model property:
from flask.ext.appbuilder.models.decorators import renders
class MyModel(Model):
id = Column(Integer, primary_key=True)
name = Column(String(50), unique = True, nullable=False)
custom = Column(Integer(20))
@renders('custom')
def my_custom(self):
# will render this columns as bold on ListWidget
return Markup('<b>' + custom + '</b>')
On your view reference your method as a column on list:
class MyModelView(ModelView):
datamodel = SQLAInterface(MyTable)
list_columns = ['name', 'my_custom']
Upvotes: 1