python - odoo如何通过更新另一个字段的值来更新一个字段的值,两个字段都在不同的类中
问题描述
我对 odoo 很陌生,我想更新 and 的值,discount_type
因为discount
我更新或更改global_discount_type
and的值。我怎样global_order_discount
才能实现这一点,因为这两个变量都来自不同的类。我无法使用一般的 python 方法。请指导我。
class PurchaseOrder(models.Model):
_inherit = "purchase.order"
total_discount = fields.Monetary(string='Total Discount', store=True, readonly=True, compute='_amount_all',
track_visibility='always')
global_discount_type = fields.Selection([
('fixed', 'Fixed'),
('percent', 'Percent')
], string="Discount Type", )
global_order_discount = fields.Float(string='Global Discount', store=True, track_visibility='always')
class PurchaseOrderLine(models.Model):
_inherit = "purchase.order.line"
discount = fields.Float(string='Discount', digits=dp.get_precision('Discount'), default=0.0)
discount_type = fields.Selection([
('fixed', 'Fixed'),
('percent', 'Percent')
], string="Discount Type", default='percent')
我还需要对 xml 文件进行更改吗?我想使用 onchange 来实现这一点。
解决方案
您需要使用相关字段discount_type
并关联 discount
到purchase.order.line
.purchase.order
例如:
discount = fields.Float(related="order_id.global_order_discount")
分别更新discount
和discount_type
为每一global_order_discount
行global_discount_type
:
@api.onchange('global_order_discount', 'global_discount_type')
def onchange_field(self):
for line in self.order_line:
line.discount = self.global_order_discount
line.discount_type = self.global_discount_type
推荐阅读
- c# - 无法在 C# 中创建快捷方式
- mysql - Windows 上的 MySQL 服务在每次启动时都会重置密码
- f# - Ionide VS Code 始终看到运行 dotnet build 时不会发生的错误?
- javascript - 使用 purgecss 安全列表选项会给出 UnhandledPromiseRejectionWarning
- symfony - OroPlatform:在核心实体上添加自定义字段
- google-cloud-functions - 使用 Express.js 路由将 JSON 数据发送到 Google Cloud Functions
- flutter - Flutter 提供者使用
- java - 尽管用户存在,但返回 UsernameNotFoundException
- python - 定时器阻止程序退出
- python - 使用 python IB API 堆叠数据