要求: 根据stock.picking中的客户,picking_type_code(操作类型)为外发。然后,在 move_ids(stock.picking 的 one2many 字段)中,product_id 和 lot_number 字段 Many2one 下拉列表应根据客户传入的 stock.picking 记录动态过滤。
示例: '''
@api.onchange('partner_id')
def onchange_partner_id(self):
if self.partner_id and self.picking_type_code =="outgoing":
print(f"Selected partner_id: {self.partner_id.name}")
# Filter product_ids based on the selected partner_id and picking type
domain = [
('picking_id.partner_id' , '=' , self.partner_id.id) ,
('product_id' , '!=' , False),
('lot_number' , '!=' , False)
]
# Fetch the actual lot numbers using the domain
move_records = self.env['stock.move'].search(domain)
move_records = move_records.filtered(lambda move: move.picking_id.picking_type_code == 'incoming')
lot_numbers = move_records.mapped('lot_number')
print(f"Updated Lot Numbers: {lot_numbers}")
# Extract product_ids from move_records
product_ids = move_records.mapped('product_id')
print(f"Updated Product IDs: {product_ids}")
# Return the domain for product_id filtering
return {
'domain': {
'product_id': [('id' , 'in' , product_ids.ids)],
'lot_number': [('id' , 'in' , lot_numbers.ids)]
}
}
'''
日志: 选定的partner_id:Floyd Steward
更新批号:stock.lot(34, 35, 36, 83)
更新的产品 ID:product.product(20, 21, 19, 37)
我知道这个方法已被弃用,但它应该可以正常工作吗? 正如您所看到的,变量值已正确获取,但域未获取,请任何人知道原因是什么,请评论
实际上我尝试过使用compute_field,但同样也没有获取域中的值 所以我无法解决这个问题,我一直遇到
我已经尝试过网络domain_field和pedrabeaza github解决方案(compute_field)如果您有任何不同的解决方案也请评论
我使用计算方法解决了这个问题,实际上我之前也尝试过计算方法,但我遇到了一些问题,我对其进行了故障排除。只需使用相同的逻辑,只需更改计算方法并使用 @api.depends 即可。将我在此处使用域返回的值分配给many2many字段,然后将该字段添加到product_id和lot_number字段定义的域属性内。这会起作用..