Skip to Content
Menu
This question has been flagged
2 Replies
857 Views

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?

Avatar
Discard
Best Answer

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

Avatar
Discard
Best Answer

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.

Avatar
Discard
Related Posts Replies Views Activity
1
May 25
628
1
May 25
668
2
Mar 25
712
1
Mar 25
706
1
Feb 25
761