Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
2 Trả lời
1557 Lượt xem

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?

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

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

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

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.

Ảnh đại diện
Huỷ bỏ
Bài viết liên quan Trả lời Lượt xem Hoạt động
3
thg 9 25
539
1
thg 9 25
1951
1
thg 8 25
386
0
thg 7 25
1188
1
thg 5 25
1908