gpt4 book ai didi

python - 将动态域应用于 Odoo8 中的 many2many 字段的最佳方法?

转载 作者:太空宇宙 更新时间:2023-11-04 05:27:16 25 4
gpt4 key购买 nike

我有一个模型,用户可以在其中选择狗的品种(dog_breed,一个选择字段)。然后,在同一模型中,我有一个 Many2many 字段 (dogs),用户可以在其中添加狗。但我希望当用户添加一只狗时,他只能在 Selection 字段中选择品种的狗中进行选择。

<field name="dog_breed"/>
<field name="dogs" domain="[('breed', '=', dog_breed)]"/>

当您将元素添加到 Many2many 时,首先您会看到可用记录的列表,如果您要查找的记录已经存在,则可以选择其中之一。我需要的是将域应用于该列表。

Example:

An user selects foxterrier in the Selection field dog_breed. Now he adds an element in the Many2many dogs. A list with all the available dogs will be opened, but I want to show a list with all the available foxterriers. To do that, I wrote the above code, but it is not working at all.

但是,如果我这样修改代码:

<field name="dog_breed"/>
<field name="dogs" domain="[('breed', '=', 'foxterrier')]"/>

它工作完美。是否可以通过 XML 代码对此进行管理?

最佳答案

动态域必须在 Python 代码中应用,在这种情况下甚至不需要使用 onchanges 来返回所需的域,我只需要将我的域剪切到Python 中的 Many2many 字段:

dogs = fields.Many2many(
comodel_name='dog',
relation='mss_dog_rel',
column1='mss_id',
column2='dog_id',
string='Dogs',
domain="[('breed', '=', dog_breed)]",
)

关于python - 将动态域应用于 Odoo8 中的 many2many 字段的最佳方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38326470/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com