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

2024-04-26 00:10:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在标准价格的计算字段中得到主公司的标准价格值。 我尝试使用此代码,但始终显示“False”值

有什么帮助吗

这是我的密码:

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_价格:假

谢谢


Tags: selfapiids标准contexttemplate价格product
1条回答
网友
1楼 · 发布于 2024-04-26 00:10:11

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

相关问题 更多 >