ODOO 按日期分组

 

<filter string="日期" name="group_by_check_in_day" context="{'group_by':'check_in:day'}"/>

我有个字段 check_in 是datetime类型的,我想按字段的date分组,也就是按日期,忽略掉时间

我查看odoo源码,看odoo是怎么实现按日期分组的,我看到:

<filter string="Date" name="groupby_date" domain="[]" context="{'group_by':'date:day'}"/>

<filter string="Creation Date" name="creation_month" context="{'group_by':'create_date:month'}" help="Creation Date"/>

我原以为在字段后加上 [ :day ] 就可以按天分组了 ,加上 [ :month ] 就可以按月份分组了

<filter string="日期" name="group_by_check_in_day" context="{'group_by':'check_in:day'}"/>

而我重启服务升级之后,得到的效果确实这样的:

并不是单纯的按月份 或天 进行的分组,当我尝试写成下面这样:

<filter string="日期" name="group_by_check_in_day" context="{'group_by':'check_in'}"/>

发现写的效果和不写的效果是一样的,下面是我搜出来的源码:

    ``context``
        a Python dictionary, merged into the action's domain to generate the
        search domain

        The key ``group_by`` can be used to define a groupby available in the
        'Group By' menu.
        The 'group_by' value can be a valid field name.

        .. code-block:: xml

          <filter name="groupby_category" string="Category" context = {'group_by': 'category_id'}/>

        The groupby defined above allows to group data by category.

        When the field is of type ``date`` or ``datetime``, the filter generates a submenu of the Group By
        menu in which the following interval options are available: day, week, month, quarter, year.

        In case the filter is in the default set of filters activated at the view initialization,
        the records are grouped by month by default. This can be changed by using the syntax
        'date_field:interval' as in the following example.

        Example:

        .. code-block:: xml

          <filter name="groupby_create_date" string="Creation Date" context = {'group_by': 'create_date:week'}/>

大致的翻译一下:

``上下文''
        Python字典,合并到操作的域中以生成搜索域

        键“ group_by”可用于定义“分组依据”菜单中可用的分组依据。
        “ group_by”值可以是有效的字段名称。

        ..代码块:: xml

          <filter name =“ groupby_category” string =“类别” context = {'group_by':'category_id'} />

        上面定义的groupby允许按类别对数据进行分组。

        当字段的类型为``date''或``datetime''时,过滤器将生成Group By菜单的子菜单,
        该菜单中提供以下间隔选项:日,周,月,季度,年。

        如果过滤器是在视图初始化时激活的默认过滤器集中,则默认情况下按月对记录进行分组。
        可以通过使用语法来更改,如以下示例中的“ date_field:interval”。

        例:

        ..代码块:: xml

          <过滤器名称=“ groupby_create_date”字符串=“创建日期”上下文= {'group_by':'create_date:week'} />

 从第13行和第14行可以看到,当字段的类型为``date''或``datetime''时,过滤器将生成Group By菜单的子菜单,该菜单中提供以下间隔选项:日,周,月,季度,年。

OVO 对对对,没错是个带子菜单的菜单

继续往下看,看第16行和第17行,OVO 明白了明白了,得有个默认激活呀

光有下面这一行是不行的呀

<filter string="日期" name="group_by_check_in_day" context="{'group_by':'check_in'}"/>

得在action加一个激活默认分组的上下文

<field name="context">{'search_default_group_by_check_in_day':1}</field>

重启服务,升级模块,有了,而且是默认按月分组的

好,我改成按日期分组

<filter string="日期" name="group_by_check_in_day" context="{'group_by':'check_in:day'}"/>

果然,按日期分组了

 


接下来做个总结

1 想要将Datetime类型的字段按日、周、月、季或年分组,需要在分组字段后加上 :internal ,internal 有如下:

:day	    日
:week	    周
:month	    月(默认)
:quarter    季
:year	    年

2 要默认分组必须在action中加一个上下文

<field name="context">{'search_default_group_by_check_in_day':1}</field>
需要以 search_default_ 开头,后接 filter标签的name值

3 最后,多扒源码

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值