python - 填充 Many2many 字段(odoo 8)

标签 python xml postgresql openerp odoo

我做了什么:

我有一个带有

的模块
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/

相关文章:

java - 当我使用 crontab 调用 Python 脚本时,进程不会终止

python - 使用docutils从重组文本中的代码指令中提取代码

javascript - 使用javascript在 Canvas 中创建缩略图

python - 分组依据后的注释最大值

python - 切片列表以获得相同长度的四分之一

html - 为什么我的 XPath 与正则表达式无法匹配?

java - 在 Dom 中创建新 child

ruby-on-rails - 列 "users.id"必须出现在 GROUP BY 子句中或用于聚合函数中

mysql - 在 mysql workbench 中启动迁移向导时出现问题

ruby-on-rails - 为什么 `execute` 返回到 shell 而不是直接将 SQL 发送到我的 Postgres 服务器?