【问题标题】:How to Show Date into words in odoo 12 reports如何在 odoo 12 报告中将日期显示为文字
【发布时间】:2020-10-21 20:44:35
【问题描述】:

我正在使用 odoo 12 并希望将模型中的日期字段记录显示到报告中,我该怎么做? 仍然面临错误 time data 'False' does not match format '%d %B, %Y'

这是我的代码

@api.multi
def _get_student_dob_in_words(self, student_id):
    
    date_of_birth = fields.Date('BirthDate', default=fields.date.today(),
                            states={'done': [('readonly', True)]})
    
    student = self.env['student.student'].search([('id','=',student_id)])
    date_of_birth = student.date_of_birth
    # date_of_birth = date.today()
    # %Y-%m-%d %H:%M:%S
    strp_date = datetime.strptime(str(date_of_birth),"%d %B, %Y")
    # dob_result = datetime.strptime(date_of_birth, DEFAULT_SERVER_DATETIME_FORMAT).date()
    # str_dob_result = str(date_of_birth)
    strp_dob_result = num2words(strp_date)
    strp_dob_result = strp_dob_result.upper()
   
    return [{
            'dob_in_words': strp_dob_result}]

【问题讨论】:

    标签: python odoo odoo-12


    【解决方案1】:

    你根本不需要strptime,你可以使用strftime。您仍然需要测试日期是否存在。我也会把它作为计算域。

    _{name|inherit} = 'student.student'
    
    #date_of_birth = fields.Date(...
    date_of_birth_words = fields.Char(compute='_compute_dob_in_words', store=True)
    
    @api.depends('date_of_birth')
    def _compute_dob_in_words(self):
        for student in self:
            if student.date_of_birth:
                date_of_birth = student.date_of_birth
                
                month = date_of_birth.strftime('%B') #date_of_birth.month
                day = num2words.num2words(date_of_birth.day, to='ordinal')
                year = num2words.num2words(date_of_birth.year)
                student.date_of_birth_words = "{} {} in year {}".format(day, month, year).upper()
    

    【讨论】:

      猜你喜欢
      • 2019-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多