首页 > 解决方案 > 如何从“ir.property”模型中获取价值。奥多 14

问题描述

我想在standard_price的计算字段中获取主要公司的standard_price的值。我尝试使用此代码,但始终显示“假”值。

请问有什么帮助吗??

这是我的代码:

class ProductTemplate(models.Model):
    _inherit = "product.template"
    @api.depends_context('company')
    @api.depends('product_variant_ids', 'product_variant_ids.standard_price')
    def _compute_standard_price(self):
        st_price = self.env['ir.property']._get(self.with_context(company=self.env.ref('base.main_company')).standard_price,"product.product")
        _logger.info('------st_price:%s', st_price)

        # Depends on force_company context because standard_price is company_dependent
        # on the product_product
        unique_variants = self.filtered(lambda template: len(template.product_variant_ids) == 1)
        for template in unique_variants:
            template.standard_price = template.product_variant_ids.standard_price
        for template in (self - unique_variants):
            template.standard_price = 0.0

结果: ------st_price:False

谢谢

标签: pythonodoo

解决方案


您需要force_company用作上下文标志并“重新加载”记录:

main_company = self.env.ref('base.main_company')
main_company_templates_prices = {
    t.id:t.standard_price for t in 
    self.with_context(force_company=main_company.id)
}
for template in self:
    main_company_standard_price = main_company_templates_prices.get(
        template.id, template.standard_price)
    # do something

推荐阅读