【问题标题】:Sum selected records in one2many field odoo12对 one2many 字段 odoo12 中的选定记录求和
【发布时间】:2020-02-16 08:54:10
【问题描述】:

我有以下型号

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
class ProductionData(models.Model):

   _name = 'dlc.pdata'

   _rec_name = 'start_date'

   _description = 'New Description'



name = fields.Char()

start_date = fields.Date(string="From", required=False, )

end_date = fields.Date(string="To", required=False,)

production_details_ids = fields.One2many(comodel_name="dlc.pdetails", inverse_name="production_data_id", string="Production Details"
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, )

使用该字段 sum_production 为我提供了每个工作站的总产量,但现在我想要过去 7 天的总产量。

【问题讨论】:

    标签: python dns odoo-12 one2many


    【解决方案1】:

    只需过滤您用来求和的 production_ids 列表。

    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())
    

    【讨论】:

      猜你喜欢
      • 2022-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多