odoo开发笔记--前端搜索视图--按照时间条件筛选
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>
注意:
create_date字段,和create_uid字段一样,都是odoo系统自带的字段,不需要在模型中创建,视图层可以直接引用,但是需要在前端列表视图中加载显示,如果界面上确实不想显示该创建时间字段,那么可以在列表tree视图中该字段下加属性:invisible="1",就可以实现加载但隐藏的效果。
相关地址:
https://www.cnblogs.com/chjbbs/p/5575085.html
http://www.odoov.com/index.php?title=%E4%BD%BF%E7%94%A8%E6%97%B6%E9%97%B4%E5%92%8C%E6%97%A5%E6%9C%9F
http://www.cnblogs.com/dancesir/p/6893224.html
https://www.cnblogs.com/kfx2007/p/6008508.html
https://www.zhiyunerp.com/forum/erp-1/question/odoo-354
*******************************
按照时间分组:
<!--<group expand="0" string="Group By...">--> <!--<separator orientation="vertical" />--> <!--<filter name="group_by_hour" string="Creation date (hour)" domain="[]" context="{'group_by':'start_date_hour'}"/>--> <!--<filter name="group_by_day" string="Creation date (day)" domain="[]" context="{'group_by':'start_date:day'}"/>--> <!--<filter name="group_by_week" string="Creation date (week)" domain="[]" context="{'group_by':'start_date:week'}"/>--> <!--<filter name="group_by_month" string="Creation date (month)" domain="[]" context="{'group_by':'start_date:month'}" />--> <!--<filter name="group_by_year" string="Creation date (year)" domain="[]" context="{'group_by':'start_date:year'}"/>--> <!--</group>-->