目前我们的Odoo CRM上的日期格式类似于11/20/2017 13:03:41
,但我希望将格式显示为July 3rd 3:00PM
,将其显示为3 Jul 3:00PM
我们以
的形式显示此字段<field name="pickup_time"/>
我已经搜索了很多以找到如何更改格式,但它主要是在本地设置中进行更改,并且它永远是一个设置,适用于所有地方。哪个不能解决我们想要的问题,比如为不同的地方设置两种不同的格式。
答案 0 :(得分:3)
沿Odoo使用的默认常量日期格式为DEFAULT_SERVER_DATETIME_FORMAT
。这在文件misc.py
DEFAULT_SERVER_DATE_FORMAT = "%Y-%m-%d"
DEFAULT_SERVER_TIME_FORMAT = "%H:%M:%S"
DEFAULT_SERVER_DATETIME_FORMAT = "%s %s" % (
DEFAULT_SERVER_DATE_FORMAT,
DEFAULT_SERVER_TIME_FORMAT)
因此,如果您将字段声明为以下代码,那么将使用该常量:
pickup_time = fields.Datetime(
string="Pickup time",
)
因此,如果您想使用其他格式,可以使用该自定义格式创建计算字段。您需要使用一些日期函数:strftime
(对象到字符串)和strptime
(字符串到对象)。格式代码几乎在this python documentation page
from datetime import datetime
from odoo.tools import DEFAULT_SERVER_DATETIME_FORMAT
[...]
pickup_time_formated = fields.Char( # it is going to be a readonly field
string='Pickup time formated',
compute='_compute_pickup_time_formated'
)
@api.multi
@api.depends('pickup_time')
def _compute_pickup_time_formated(self):
for record in self:
date = record.pickup_time
date_obj = datetime.strptime(date, DEFAULT_SERVER_DATETIME_FORMAT) # to convert it in a date object
v = date_obj.day % 10 # to get the remainder of the operation
if v == 1:
ordinal_suffix = 'st'
elif v == 2:
ordinal_suffix = 'nd'
elif v == 3:
ordinal_suffix = 'rd'
else:
ordinal_suffix = 'th'
# format 'July 3rd 3:00PM'
record.pickup_time_formated = datetime.strftime(date_obj, '%B %d' + ordinal_suffix + ' %I:%M %p')
# format '3 Jul 3:00PM'
# record.pickup_time_formated = datetime.strftime(date_obj, '%d %b %I:%M %p'),
然后您可以在xml表单中显示新字段:
<field name="pickup_time_formated"/>