我做了什么:
我有一个带有
的模块myfield = fields.Many2one('res.partner', string="Graduate", domain=[('is_graduated', '=', True)])
然后我有另一个类(class)
_inherit = 'res.partner'
is_graduated = fields.Boolean("Graduated before?", default=False)
graduations = fields.Many2many('my_module.courses', string="Graduation courses")
我得到了什么:
myfield
效果很好,但是 graduations
字段为空。如果您编辑 user 1
个人资料,您可以将条目添加到 graduation
字段使用 Add item
,但我需要自动填写。
我的期望:
我希望myfield
的每条记录设置为 user 1
, 将在字段 graduations
中可见当你打开 user 1
轮廓。当我创建记录并设置 myfield
值(value)让我们说user 1
,该记录必须在 user 1
中可见现场简介graduations
.如何实现?
最佳答案
user_rel_ids = fields.Many2many(comodel_name='course', 关系='user_course_rel', column1='user_id', column2='course_id')
或者
user_rel_id = fields.Many2many('course')
用于填充数据(用于添加新关系)
user_rel_id = [(4,course_id)]
根据http://odoo4u.blogspot.com/2014/10/orm-methods.html , 它说: 完整的选项列表在该类的文档中。 同样的事情也适用于 one2many
For a many2many and one2many field, a list of tuples is expected. Here is the list of the tuple that is accepted, with the corresponding semantics:
(0, 0, { values })
link to a new record that needs to be created with the given values dictionary
(1, ID, { values })
update the linked record with id = ID (write values on it)
(2, ID)
remove and delete the linked record with id = ID (calls unlink on ID, that will delete the object completely, and the link to it as well)
(3, ID)
cut the link to the linked record with id = ID (delete the relationship between the two objects but does not delete the target object itself)
(4, ID)
link to existing record with id = ID (adds a relationship)
(5)
unlink all (like using (3, ID) for all linked records)
(6, 0, [IDs])
replace the list of linked IDs (like using (5) then (4,ID) for each ID in the list of IDs)
关于python - 填充 Many2many 字段(odoo 8),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31853402/