Přejít na obsah
Menu
You need to be registered to interact with the community.
This question has been flagged
2 Odpovědi
1215 Zobrazení

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
Zrušit
Nejlepší odpověď

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
Zrušit
Nejlepší odpověď

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
Zrušit
Related Posts Odpovědi Zobrazení Aktivita
1
kvě 25
1037
ADD PROPERTIES Vyřešeno
1
kvě 25
1089
2
bře 25
1048
1
bře 25
1097
1
úno 25
1141