Odoo动态many2one域 [英] Odoo dynamic many2one domain

查看:66
本文介绍了Odoo动态many2one域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将动态过滤器应用于基于另一个字段(F2)的many2one字段(F1).我已经使用 @ api.change 装饰器完成了此操作,并且可以正常使用,但效果不理想.

I want to apply dynamic filter to a many2one field (F1) based on another field (F2). I've done that using @api.change decorator, and it works but not as expected.

当我创建一个新实体时,我更改F2的值,然后转到F1,我发现它已归档,好吧,没问题.

When i create a new entity, i change the value of F2, then go to F1, i find it filered, Oki no problem.

当我关闭表单,然后再次对其进行编辑,然后直接进入F1字段时,我再次获得所有可用的可能性(未过滤),我需要先转到F2,然后选择相同的值(已经以前选择),然后返回F1.

When i close the form, and then edit it again, and i go directly to the F1 field, i get again all available possibilities (not filtered), i need first to go to F2 and then choose the same value (already chosen previously) and then come back to F1.

有什么主意吗?(在代码下面:F1 =继承的product_id,F2 = bom_id)

Any idea ? (below the code: F1 = product_id which is inherited, and F2 = bom_id)

class ProductionLot(models.Model):
    _inherit = "stock.production.lot"

    company_id = fields.Many2one(default=lambda self: self.env['res.company']._company_default_get('account.invoice'))

    bom_serial_number_line_ids = fields.One2many("mrp.bom.serialnumber.line", "parent_lot_id", "BoM Serial Numbers")
    bom_id = fields.Many2one("mrp.bom", "BoM")

    @api.onchange('product_id')
    def update_bom_id_from_product_id(self):
        for record in self:
            if (record.product_id):
                bom_complex_kit = record.product_id.env['mrp.bom']._bom_find(
                    product_tmpl=record.product_id.product_tmpl_id,
                    bom_type='complex_kit')

                self.bom_id = bom_complex_kit

            return {"domain": {"bom_id": [('product_tmpl_id.id', '=', record.product_id.product_tmpl_id.id),
                                          ('type', '=', 'complex_kit')]}}

推荐答案

由于onchange过滤器仅在函数为触发器时才适用,因此仅当您更改on change值时它才起作用,我想您需要做的是

As onchange filter will only be applied when the function is a trigger so it will only work when you change the on change value I guess what you need to do is this or combination of both onchange and default domain on field

def get_domain(self):
    ids = self.env['stock.production.lot'].browse(self._context.get('active_ids'))
    print("Here see all ids and use them accordingly",ids) 

bom_id = fields.Many2one("mrp.bom", "BoM", domain = lambda self:self.get_domain())

这篇关于Odoo动态many2one域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆