I want to change the default way dates are presented to me in the flask admin, to give it a specific timezone and display it in a more human-readable format.
我想更改在刻录机管理员中显示日期的默认方式,为其提供特定的时区并以更易于阅读的格式显示。
There are a number of ways of going about this (filters, __html__
, __str__
, Babel, etc), but while these could work, my question is whether there is a more general approach for a more general problem. The specific problem of formatting dates is just an example.
有很多方法可以解决这个问题(过滤器,__ html__,__str __,Babel等),但是虽然这些方法可行,但我的问题是,对于更普遍的问题是否有更通用的方法。格式化日期的具体问题只是一个例子。
In my scenario, I don't have control over the date object -- I can't subclass it or monkeypatch a __str__
or __html__
method. I want it to happen automatically for all dates in the templates, and I don't want to have to write custom admin templates, and I don't want to have to use explicit filters in my templates for this scenario.
在我的场景中,我无法控制日期对象 - 我不能将其子类化或monkeypatch __str__或__html__方法。我希望它自动发生在模板中的所有日期,我不想编写自定义管理模板,我不想在我的模板中使用显式过滤器来实现此方案。
My ideal solution would be to somehow specify a default filter to Jinja, so that all data was passed through that filter before being presented. I can write the filter myself, but I can't see how to get Jinja to use it.
我理想的解决方案是以某种方式为Jinja指定一个默认过滤器,以便所有数据在呈现之前通过该过滤器。我自己可以编写过滤器,但我看不出如何让Jinja使用它。
One thought I had was to use autoescaping somehow (see this question), but I can't see any way to override Jinja's autoescaping functionality without nasty monkeypatching.
我有一个想法是以某种方式使用自动转换(请参阅此问题),但我看不出任何方法来覆盖Jinja的自动转换功能而没有令人讨厌的monkeypatching。
Any ideas?
有任何想法吗?
2 个解决方案
#1
1
If you want that in Flask-Admin related code, you can rely on column_type_formatters
.
如果您想在Flask-Admin相关代码中使用它,则可以依赖column_type_formatters。
https://flask-admin.readthedocs.org/en/latest/api/mod_model/#flask.ext.admin.model.BaseModelView.column_type_formatters
#2
0
You don't have to monkeypatch anything.
你不需要做任何东西。
You just create a custom base class for your Model views with custom formatters. An example for date
class:
您只需使用自定义格式化程序为模型视图创建自定义基类。日期类的示例:
from datetime import date
from flask_admin.model import typefmt
def date_format(view, value):
return value.strftime('%d.%m.%Y')
MY_DEFAULT_FORMATTERS = dict(typefmt.BASE_FORMATTERS)
MY_DEFAULT_FORMATTERS.update({
date: date_format,
})
class MyModelView(BaseModelView):
column_type_formatters = MY_DEFAULT_FORMATTERS
#1
1
If you want that in Flask-Admin related code, you can rely on column_type_formatters
.
如果您想在Flask-Admin相关代码中使用它,则可以依赖column_type_formatters。
https://flask-admin.readthedocs.org/en/latest/api/mod_model/#flask.ext.admin.model.BaseModelView.column_type_formatters
#2
0
You don't have to monkeypatch anything.
你不需要做任何东西。
You just create a custom base class for your Model views with custom formatters. An example for date
class:
您只需使用自定义格式化程序为模型视图创建自定义基类。日期类的示例:
from datetime import date
from flask_admin.model import typefmt
def date_format(view, value):
return value.strftime('%d.%m.%Y')
MY_DEFAULT_FORMATTERS = dict(typefmt.BASE_FORMATTERS)
MY_DEFAULT_FORMATTERS.update({
date: date_format,
})
class MyModelView(BaseModelView):
column_type_formatters = MY_DEFAULT_FORMATTERS