如何将日期转换为 Odoo 模块中的格式化字符串?

How to trasnform date to formatted string in a Odoo module?

有一个填写当前日期的日期字段。另一方面,我有一个计算字段,我需要成为这个已生成的日期,但使用自动生成字段的格式为 yyyymmddhhmmss。有 de class:

class incidencia(models.Model):
    _name = 'incidencias.incidencia'

    fecha_inicio = fields.Date(default=fields.Date.today, required=True)
    name = fields.Char(compute='_asignar_nombre', required=True)

    @api.depends('fecha_inicio')
    def _asignar_nombre(self):
        #¿?

有什么想法吗?我刚开始使用 Odoo,我不确定如何处理数据类型。

class incidencia(models.Model):
    _name = 'incidencias.incidencia'

    fecha_inicio = fields.Date(default=fields.Date.today, required=True)
    name = fields.Char(compute='_asignar_nombre', required=True)

    @api.depends('fecha_inicio')
    def _asignar_nombre(self):
        for record in self:
            record.name = fields.Date.from_string(
                record.fecha_inicio).strftime('%Y%m%d%H%M%S')

注意:如您选择Date instead of Datetime, you will get zeros for hours, minutes and seconds in the formatted name, as time information is not stored in a Date field. If you need to keep track of time as well, I would suggest to use Datetime. Note to change to odoo.fields.Datetime.from_string in the compute function if you decide to use Datetime.