odoo group by 彙總功能

那时一个人發表於2024-03-06

彙總欄位屬性有哪些:

# valid SQL aggregation functions
VALID_AGGREGATE_FUNCTIONS = {
    'array_agg', 'count', 'count_distinct',
    'bool_and', 'bool_or', 'max', 'min', 'avg', 'sum',
}

count_distinct 使用:

class AppSaleReport(models.Model):
    _name = "app.sale.report"
    _description = "App Sale Report"
    _table = "view_app_sale_report"
    _auto = False

    id = fields.Integer("ID")
    partner_id = fields.Many2one("res.partner", "Customer", group_operator="count_distinct")
    date_order = fields.Datetime("Order Date")
    name = fields.Char("Order Reference")
    product_id = fields.Many2one("product.product", "Product")
    price_subtotal = fields.Float("Subtotal")
    count_active_user = fields.Integer("Activity User")
    company_id = fields.Many2one("res.company", "Company")

    # group by date_order 獲取每週下單的活躍使用者數
    @api.model
    def read_group(self, domain, fields, groupby, offset=0, limit=None, orderby=False, lazy=True):
        fields.append('partner_id:count_distinct')
        res = super(AppSaleReport, self).read_group(domain, fields, groupby, offset, limit, orderby, lazy)
        if 'count_active_user' in fields:
            for line in res:
                if line.get('partner_id'):
                    line['count_active_user'] = line.get('partner_id')
        return res

image

相關文章