Python 对one2many字段odoo12中选定的记录求和
我有以下型号Python 对one2many字段odoo12中选定的记录求和,python,dns,odoo-12,one2many,Python,Dns,Odoo 12,One2many,我有以下型号 class WorkStation(models.Model): _name = 'dlc.workstation' _description = 'list of work station' production_ids = fields.One2many(comodel_name="dlc.pdetails", inverse_name="workstation_id", string="Production", required=False, ) su
class WorkStation(models.Model):
_name = 'dlc.workstation'
_description = 'list of work station'
production_ids = fields.One2many(comodel_name="dlc.pdetails", inverse_name="workstation_id", string="Production", required=False, )
sum_production = fields.Integer(string="Production total", compute='_dlc_production', required=False,)
@api.one
@api.depends('production_ids.total', )
def _dlc_production(self):
self.sum_production = sum(production.total for production in self.production_ids)
"""
@api.depends() should contain all fields that will be used in the calculations.
"""
pass
通过此字段,sum_production可为我提供每个工作站的总产量数据,但现在我需要过去7天的总产量。只需筛选用于求和的生产ID列表即可
from datetime import date
@api.one
@api.depends('production_ids.total', )
def _dlc_production(self):
production_list = self.production_ids.filter(lambda r: r.date and (date.today() - timedelta(days=7)) <= r.date <= date.today())
self.sum_production = sum(production.total for production in production_list)
from datetime导入日期
@api.1
@api.dependens('production_id.total',)
def_dlc_生产(自):
production\u list=self.production\u id.filter(lambda r:r.date和(date.today()-timedelta(days=7))
class ProductionDetails(models.Model):
_name = 'dlc.pdetails'
_rec_name = 'workstation_id'
workstation_id = fields.Many2one(comodel_name="dlc.workstation", string="DLC", required=False, )
production_data_id = fields.Many2one(comodel_name="dlc.pdata", string="Production", required=False, )
card_type = fields.Selection(string="Card Type",
selection=[('Temporary', 'Temporary'), ('Office Total', 'Office Total'),
('Permanent', 'Permanent')], required=False, )
date = fields.Date(string="Date", required=False, related='production_data_id.start_date')
total = fields.Integer(string="TOTAL", required=False, )
from datetime import date
@api.one
@api.depends('production_ids.total', )
def _dlc_production(self):
production_list = self.production_ids.filter(lambda r: r.date and (date.today() - timedelta(days=7)) <= r.date <= date.today())
self.sum_production = sum(production.total for production in production_list)
workstation_list = self.env['dlc.workstation'].search([])
production_list = workstation_list.mapped('production_ids').filter(lambda r: r.date and (date.today() - timedelta(days=7)) <= r.date <= date.today())