Siirry sisältöön
Menu
Sinun on rekisteröidyttävä, jotta voit olla vuorovaikutuksessa yhteisön kanssa.
Tämä kysymys on merkitty
2 Vastaukset
1558 Näkymät

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
Hylkää
Paras vastaus

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
Hylkää
Paras vastaus

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
Hylkää
Aiheeseen liittyviä artikkeleita Vastaukset Näkymät Toimenpide
3
syysk. 25
539
1
syysk. 25
1951
1
elok. 25
386
0
heinäk. 25
1188
1
toukok. 25
1908