【问题标题】:Odoo: on create save current ID in a many2one field of another modelOdoo:在创建时在另一个模型的 many2one 字段中保存当前 ID
【发布时间】:2020-09-14 11:15:09
【问题描述】:

我正在尝试为产品创建一个custom.modify.price 模型,我可以在其中选择特定标准来选择产品,然后我填写一个影响所有先前选择的价格。我希望它为选择中的每个产品,在另一个模型custom.sale.price 中创建一个新记录,并且每条记录都应该包含在many2one 字段中创建它的custom.modify.price 记录的ID。问题是我可以通过覆盖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, )


【问题讨论】:

  • 调用super,然后尝试使用创建记录的id (result)。
  • 你能举个例子吗
  • 创建custom.modify.price并使用结果values更新id,然后创建custom.sale.price
  • 非常感谢@Kenly,它工作得很好,请将其作为答案发布,以便我投票:)
  • 不客气,我会尝试添加一个最小的例子。

标签: python-3.x odoo-12


【解决方案1】:

您需要创建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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-14
    • 1970-01-01
    • 2022-08-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多