跳至内容
菜单
此问题已终结
2 回复
1560 查看

Hi,

I am trying to implement a filter to display records created within the last 30 minutes. Here is the code snippet I am using:


<filter

        name="wave_and_recent_creation"

        string="Wave created in Last 30 Min"

        domain="[

            ('batch_id', '!=', False),

            ('batch_creation_date', '>=', (context_today() + datetime.timedelta(minutes=30)).strftime('%Y-%m-%d %H:%M:%S'))

        ]"

/>

However, this code is not working as expected. Could you guide me on how to correct it and implement the filter properly?

形象
丢弃
最佳答案

Hi,

There are a few adjustments needed in your filter to make it work correctly. Here's the corrected version:

<filter

    name="wave_and_recent_creation"

    string="Wave created in Last 30 Min"

    domain="[

        ('batch_id', '!=', False),

        ('batch_creation_date', '>', (context_today() + relativedelta(minutes=-30)).strftime('%Y-%m-%d %H:%M:%S'))

    ]"

/>

The changes made are:

  1. Changed datetime.timedelta to relativedelta - this is the proper way in Odoo to handle time calculations
  2. Changed - to + and 30 to -30 in the time calculation - when we say "30 minutes ago" we're adding a negative value
  3. Changed >= to > for more precise filtering

Make sure you have the proper import in your Python file:

from dateutil.relativedelta import relativedelta

Regards,

--

Jishna

Accurates

形象
丢弃
最佳答案

You're on the right track. Since you need the last 30 minutes and working with time, you don't need context_today or today.. You just need the datetime.now and subtract the delta you want from it.

形象
丢弃
相关帖文 回复 查看 活动
3
9月 25
540
1
9月 25
1952
1
8月 25
387
0
7月 25
1188
1
5月 25
1908