You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Odoo日期过滤器未按预期工作问题求助

Fixing the "Today" Filter for Date-Type Fields

Let me walk you through why your filter isn't working and how to fix it quickly:

The Root Problem

Your database's date field is a pure date type (not datetime), but your filter's domain uses datetime.datetime.combine() to create datetime objects for comparison. When working with frameworks like Odoo (given your syntax), comparing a date-only field against datetime values can trigger unexpected type conversion issues—even if your server time and timezone settings are correct. The ORM won't interpret the datetime range as intended for a date-only column.

Simple Fixes to Try

1. Use Direct Date Matching (Recommended)

Since your date field stores only dates, you don't need to mess with time ranges at all. Just compare directly to context_today(), which returns a date object:

<filter name="today" string="Today" domain="[('date', '=', context_today())]" />

This will match all records where the date field exactly equals the current day—no extra time logic needed.

2. Verify Default Filter Activation

Double-check that your search_default_today:1 is correctly set in the right context. If you're defining it in an action, make sure it's in the action's context field:

<field name="context">{'search_default_today': 1}</field>

If it's in the view's context, ensure the view definition includes it properly—typos or misplaced context entries can easily prevent the default filter from activating.

3. If You Must Use a Range (For Edge Cases)

If for some reason you need to stick with a range query (though it's unnecessary for date-type fields), adjust the domain to use date objects instead of datetimes:

<filter name="today" string="Today" domain="[('date', '&gt;=', context_today()), ('date', '&lt;=', context_today())]" />

This achieves the same result as the first fix but makes the range explicit.

Quick Debug Tip

To confirm the issue, enable debug mode in your framework, run the filter, and check the generated SQL query (usually under a "Debug" menu > "View SQL Query"). You'll likely see that the original datetime-based domain was trying to compare dates to datetime strings, which doesn't work as intended for date-only columns.

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

火山引擎 最新活动