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

I have a datetime field in my sale order, how i add filter by in portal list view , for filtering last week,last monthe,last year using python controller.How i add domain in this code .

searchbar_filters = {
'today': {
'label': 'Today',
'domain': []
},
'week': {
'label': 'Last Week',
'domain': []
},
'month': {
'label': 'Last Month',
'domain': []
},
'year': {
'label': 'Last Year',
'domain': []
}
}



Avatar
Discard
Best Answer

Hi,

from dateutil.relativedelta import relativedelta
from odoo.tools import date_utils

write this code in your controller:


today = fields.Date.today()
last_week = today + relativedelta(weeks=-1)
last_month = today + relativedelta(months=-1)
last_year = today + relativedelta(years=-1)


searchbar_filters = {
'today': {
'label': 'Today',
'domain': [("date", ">=",
fields.Datetime.to_string(fields.Datetime.today())), (
"date", "<=", fields.Datetime.to_string(
fields.Datetime.today().replace(hour=23, minute=59,
second=59)))]
},
'week': {
'label': 'Last Week',
'domain': [('date', '>=', date_utils.start_of(last_week, "week")),
('date', '<=', date_utils.end_of(last_week, 'week'))]
},
'month': {
'label': 'Last Month',
'domain': [('date', '>=', date_utils.start_of(last_month, 'month')),
('date', '<=', date_utils.end_of(last_month, 'month'))]
},
'year': {
'label': 'Last Year',
'domain': [('date', '>=', date_utils.start_of(last_year, 'year')),
('date', '<=', date_utils.end_of(last_year, 'year'))]
}
}

Regards

Avatar
Discard
Best Answer

You can add the domains as below ( Replace 'Your_datetime_field' with your datetime field:

searchbar_filters = {
'all': {'label': _('All'), 'domain': []},
'today': {'label': _('Today'), 'domain': [("Your_datetime_field", ">=", fields.Datetime.to_string(fields.Datetime.today())),("Your_datetime_field", "<=", fields.Datetime.to_string(fields.Datetime.today().replace(hour=23, minute=59, second=59)))]},
'last_week': {'label': _('Last week'), 'domain': [('Your_datetime_field', '>=', date_utils.start_of(last_week, "week")),
('Your_datetime_field', '<=', date_utils.end_of(last_week, 'week'))]},
'last_month': {'label': _('Last month'),
'domain': [('Your_datetime_field', '>=', date_utils.start_of(last_month, 'month')),
('Your_datetime_field', '<=', date_utils.end_of(last_month, 'month'))]},
'last_year': {'label': _('Last year'), 'domain': [('Your_datetime_field', '>=', date_utils.start_of(last_year, 'year')),
('Your_datetime_field', '<=', date_utils.end_of(last_year, 'year'))]},
}

Also you need to import the below:

from odoo.tools import date_utils

Avatar
Discard
Related Posts Replies Views Activity
1
May 25
2411
1
Apr 25
3428
1
Apr 25
4247
1
Apr 25
1744
4
Mar 25
6844