如何解决此错误 - 未捕获错误:'2016-'日期、日期时间和时间不正确



我从Odoo社区参考网站获得了以下部分代码,用于创建生日日历标记

.PY文件

class birthday_report(osv.osv):
    _name = "birthday.report"
    _auto = False
    _columns = {
        'name': fields.many2one('hr.employee','Employee', readonly=True),
        'dob' : fields.date('Birthday', readonly=True),
    }
    def init(self, cr):
        tools.drop_view_if_exists(cr, 'birthday_report')
        cr.execute("""
            create or replace view birthday_report as (
            select
            h.id as id,
            h.id as name,
concat(concat(date_part('Year',current_date),'-'),to_char(h.birthday, 'mm-dd')) as dob
            from
            hr_employee as h
            join
            resource_resource as r
            on
            h.resource_id=r.id
                        where r.active ='t'
            )
        """)
birthday_report()

.XML文件

<?xml version="1.0" encoding="utf-8"?>
<openerp>
    <data>
        <record id="view_birthday_report_calendar" model="ir.ui.view">
        <field name="name">Employee Birthday</field>
        <field name="model">birthday.report</field>
        <field name="arch" type="xml">
            <calendar string="Birthday" color="name"
                date_start="dob"
                quick_add="False" avatar_model="hr.employee">
                 <field name="name"/>
            </calendar>
        </field>
        </record>
            <record model="ir.actions.act_window" id="action_birthday_view">
            <field name="name">Birthday</field>
            <field name="res_model">birthday.report</field>
            <field name="view_type">form</field>
            <field name="view_mode">calendar</field>
            <field name="view_id" eval="view_birthday_report_calendar"/>
            <field name="domain">[]</field>
            </record>

        <menuitem id="menu_birthday" name="Birthday" parent="hr.menu_hr_root" groups="base.group_user"/>
    <menuitem id="menu_view_birthday" parent="menu_birthday" action="action_birthday_view" groups="base.group_user"/>

    </data>
</openerp>

当我导航到1月或前一年12月时出现错误,出现以下错误:

未捕获错误:"2016-"不是正确的日期、日期时间或时间。

我是Odoo中sql查询的新手,任何对此有建议的人都将不胜感激。谢谢

首先,我建议使用||而不是concat。这允许您进行多个串联。

date_part('Year',current_date) || '-' || to_char(h.birthday, 'mm-dd')

其次,我建议铸造到目前为止,所以

(date_part('Year',current_date) || '-' || to_char(h.birthday, 'mm-dd'))::date

第三,如果您只使用psql并从视图中进行选择,会发生什么?是否存在格式错误的日期?或者在Python代码的其他地方发生了其他事情?

最新更新