python - 如何从“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
推荐阅读
- java - 斯卡拉火花引起:java.lang.NoSuchMethodError:net.jpountz.lz4.LZ4BlockInputStream。
(Ljava/io/InputStream;Z)V - r - 切换R中的一些向量
- google-bigquery - 使用 BigQuery 公共数据集作为源时权限被拒绝
- javascript - 使用 JavaScript 编写前端和 Java 编写后端的 Android 应用是否算作原生应用?
- angular - 在 Typescript 中仅获取逗号分隔字符串中的动态 JSON 值
- sql - 将基数为 10 的整数转换为基数 3,加上基数为 3 的数字,并将结果转换回基数为 10
- bash - 提取与模式匹配的单词并打印字符长度
- r - 行值的增量增加,直到找到相邻列的变化
- html - 如何手动聚焦输入
- python - 通过 requests.post 将 numpy 数组(实际上是图像)发送到 API 的最佳方式(性能)是什么?(Python)