微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何从“ir.property”模型中获取价值奥多14

如何解决如何从“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

谢谢

解决方法

您需要使用 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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。