You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Odoo 14模块添加今日记录视图过滤器时出现域解析错误

Odoo 14 添加今日记录过滤器报错解决

问题描述

开发Odoo 14模块时,尝试添加查看今日记录的视图过滤器,收到错误:

Error: Control panel model extension failed to evaluate domain:/n{}

我的搜索视图代码如下:

<record id="models_cashouts_search" model="ir.ui.view">
        <field name="name">cash_in_out.request.search.view</field>
        <field name="model">cash_in_out.request</field>
        <field name="arch" type="xml">
            <search>
                <field name="appliquand_uid"/>
                <field name="amount"/>
                <field name="create_date" string="Date"/>
                <field name="acceptor_uid" />
                <filter string="Aujourd'hui" name="today" domain="[('create_date', '&gt;=', (context_today().strftime('%Y-%m-%d %H:%M:%S') - datetime.timedelta(days=1)).strftime('%Y-%m-%d %H:%M:%S')), ('create_date', '&lt;=', context_today().strftime('%Y-%m-%d %H:%M:%S'))]" />
            </search>
        </field>
</record>

错误原因

XML视图的域(domain)表达式不支持直接调用Python的datetime.timedelta模块,且context_today()的使用方式不符合Odoo域语法规范,导致Odoo无法正确解析该表达式。

解决方案

方案1:使用Odoo内置日期语法简化定义

Odoo支持直接对context_today()返回的datetime对象调整时分秒,无需引入额外Python模块:

<filter string="Aujourd'hui" name="today" domain="[('create_date', '&gt;=', context_today().replace(hour=0, minute=0, second=0)), ('create_date', '&lt;=', context_today())]" />

该写法直接获取今日零点时间,与当前时间形成区间,筛选今日创建的记录。

方案2:通过模型方法返回计算域

若需更复杂的日期逻辑,可在模型中定义方法处理域计算:

  1. 在cash_in_out.request模型中添加方法:
from datetime import datetime
from odoo import models

class CashInOutRequest(models.Model):
    _name = 'cash_in_out.request'
    # 其他字段定义...

    def _get_today_domain(self):
        today_start = datetime.now().replace(hour=0, minute=0, second=0)
        today_end = datetime.now()
        return [('create_date', '>=', today_start), ('create_date', '<=', today_end)]
  1. 在搜索视图中调用该方法:
<filter string="Aujourd'hui" name="today" domain="[_get_today_domain()]" />

验证方式

修改代码后升级对应模块,重新加载视图,点击过滤器即可查看今日创建的记录,确认错误不再出现。

内容的提问来源于stack exchange,提问作者Ezekiel Kirouni

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 04:02:19