Odoo:在另一个模型的 many2one 字段中创建保存当前 ID

Odoo: on create save current ID in a many2one field of another model

我想做的是为产品创建一个 custom.modify.price 模型,我可以在其中选择特定标准来选择产品,然后我填写影响所有先前选择的价格的百分比。我希望它为选择中的每个产品在另一个模型 custom.sale.price 中创建一个新记录,并且每个记录都应该包含在 custom.modify.price 中创建它的记录的 ID many2one 字段。问题是我可以通过覆盖 custom.modify.price create 方法并为其提供值来创建此 custom.sale.price 的新记录,但我无法为其提供当前 ID 的“尚未创建”custom.modify.price 记录

class CustomModifyPrice(models.Model):
    date = fields.Datetime(string='Date', required=True, default=fields.Datetime.now())
    brand_id = fields.Many2many(comodel_name="custom.brand", string="Brand", required=False, )
    origin_id = fields.Many2many(comodel_name="custom.country", string="Origin", required=False, )
    percentage = fields.Float(string="Percentage")
    product_ids = fields.Many2many(comodel_name="custom.product", string="Products", readonly=True, )
    
    @api.model
    def create(self, vals):
      values = {'product_id': product.id, 'date': vals['date'], 'sell_price': mod_sell_price, 'modification_id': self.id}###self.id returns nothing here
      price = self.env['custom.sale.price'].create(values)
      result = super(CustomModifyPrice, self).create(vals)
      return result

class CustomSalePrice(models.Model):
    product_id = fields.Many2one(comodel_name="custom.product", string="Product", required=False, )
    date = fields.Datetime(string="Date", default=fields.Datetime.now())
    sell_price = fields.Float(string="Selling Price")
    sale_id = fields.Many2one(comodel_name="custom.sale", string="Sales Order", required=False, )


您需要创建 custom.modify.price 并使用结果 id 更新值,然后创建 custom.sale.price

@api.model
def create(self, vals):
  result = super(CustomModifyPrice, self).create(vals)
  values = {'modification_id': result.id, ...}
  self.env['custom.sale.price'].create(values)      
  return result