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': []
}
}
Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:
- 客户关系管理
- e-Commerce
- 会计
- 库存
- PoS
- Project
- MRP
此问题已终结
2
回复
3666
查看
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
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
相关帖文 | 回复 | 查看 | 活动 | |
---|---|---|---|---|
|
3
8月 25
|
2585 | ||
|
1
5月 25
|
2630 | ||
|
1
4月 25
|
3607 | ||
|
1
4月 25
|
4480 | ||
|
1
4月 25
|
1944 |