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:
- Müşteri İlişkileri Yönetimi
- e-Commerce
- Muhasebe
- Envanter
- PoS
- Project
- MRP
Bu soru işaretlendi
            
                2
                
                    Cevaplar
                
            
        
        
            
                4175
                
                    Görünümler
                
            
        
    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
Enjoying the discussion? Don't just read, join in!
Create an account today to enjoy exclusive features and engage with our awesome community!
Üye Ol| İlgili Gönderiler | Cevaplar | Görünümler | Aktivite | |
|---|---|---|---|---|
|  | 5 Eyl 25  | 23403 | ||
|  | 3 Ağu 25  | 3509 | ||
|  | 1 May 25  | 3394 | ||
|  | 1 Nis 25  | 4294 | ||
|  | 1 Nis 25  | 5102 | 
