【问题标题】:odoo iterate over all productodoo 遍历所有产品
【发布时间】:2022-01-08 09:19:46
【问题描述】:

我想遍历所有产品并从另一个字段计算 list_price。 这是我写的代码:

class AedPriceCalculator(models.TransientModel):

_name = "aed.pricecalculator"

_inherit = ['product.template']

aed_current_price = fields.Monetary('current AED price in IRR', required=True, translate=True)
currency_id = fields.Many2one(
    'res.currency', 'Currency',default=lambda self: self.env['res.currency'].search([('name', '=', 'AED')]).id,readonly=True)



@api.multi
def price_calculate_ae(self):
        products = self.env['product.template'].search(['sale_ok', '=' ,True])
        for product in products:
            if product.amount_in_aed:
                product.list_price = self.aed_current_price * product.amount_in_aed
            else:
                pass

但是当我调用相关按钮时。 会出现这个错误:

    The operation cannot be completed:
- Create/update: a mandatory field is not set.
- Delete: another model requires the record being deleted. If possible, archive it instead.

Model: aed.pricecalculator (aed.pricecalculator), Field: Name(name)

【问题讨论】:

    标签: loops odoo


    【解决方案1】:

    aed.pricecalculator 模型继承自 product.template,并且产品模板中需要 name 字段(按原样复制)。

    当您单击按钮并调用 price_calculate_ae 方法时,Odoo 将尝试调用 create 方法而不为 name 字段提供值,这就是您看到此错误消息的原因。

    您无需继承product.template 即可遍历所有产品并计算list_price

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-11
      • 1970-01-01
      • 2016-03-29
      • 1970-01-01
      • 1970-01-01
      • 2015-03-20
      相关资源
      最近更新 更多