odoo14开发:前端搜索视图如何按照时间条件固定筛选
odoo在日常使用中,常会有这样的需要,比如,某个列表按照 日 、周、月、年来过滤搜索。
效果:
<!--某模型 搜索视图-->
<record id="view_xxxxx_search" model="ir.ui.view">
<field name="name">XXXXXXXX search</field>
<field name="model">你的模型名</field>
<field name="arch" type="xml">
<search string="xxxxxxxxxxxx search">
<field name="name" string="按照name字段搜索" domain="[('xxxxxxxx.name', '=', self)]"/>
<field name="xxxxx_id" string="按照xxxxx_id字段搜索" domain="[('xxxxxxxxx.xxxxx_id', '=', self)]"/>
<separator/>
<filter string="进口" name="in" domain="[('inout','=','i')]"/>
<filter string="出口" name="export" domain="[('inout','=','e')]"/>
<separator/>
<filter string="当天" name="today" domain="[('create_date','>=', time.strftime('%Y-%m-%d 00:00:00')),('create_date', '<', context_today().strftime('%Y-%m-%d 23:59:59'))]"/>
<filter string="本周" name="last_week" domain="[('create_date','>', (context_today() - datetime.timedelta(weeks=1)).strftime('%%Y-%%m-%%d 00:00:00'))]"/>
<filter string="本月" name="month" domain="[('create_date','>=', time.strftime('%Y-%m-01 00:00:00')),('create_date','<', (context_today() + relativedelta(months=1)).strftime('%Y-%m-01 00:00:00'))]"/>
<filter string="上月" name="month2" domain="[('create_date','<', time.strftime('%Y-%m-01 00:00:00')),('create_date','>=', (context_today() - relativedelta(months=1)).strftime('%Y-%m-01 00:00:00'))]"/>
<filter string="本年" name="year" domain="[('create_date','<=', time.strftime('%Y-12-31 23:59:59')),('create_date','>=', time.strftime('%Y-01-01 00:00:00'))]"/>
<separator/>
<filter name="过去24小时" string="Last 24h" domain="[('create_date','>', (context_today() - datetime.timedelta(days=1)).strftime('%Y-%m-%d 00:00:00') )]"/>
<filter name="上周" string="Last Week" domain="[('create_date','>', (context_today() - datetime.timedelta(weeks=1)).strftime('%Y-%m-%d 00:00:00'))]"/>
<!--另一种写法-->
<separator/>
<filter name="week" string="本周"
domain="[
'&',
('create_date', '>=', (context_today() + relativedelta(weeks=-1,days=1,weekday=0)).strftime('%Y-%m-%d')),
('create_date', '<=', (context_today() + relativedelta(weekday=6)).strftime('%Y-%m-%d')),
]"/>
<filter name="month" string="本月"
domain="[
'&',
('create_date', '>=', (context_today() + relativedelta(day=1)).strftime('%Y-%m-%d')),
('create_date', '<=', (context_today() + relativedelta(months=1, day=1, days=-1)).strftime('%Y-%m-%d')),
]"/>
</search>
</field>
</record>
文章来源:https://www.cnblogs.com/hellojesson/p/8144474.html