Python 填充 Many2many 字段 (odoo 8)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31853402/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Filling Many2many field (odoo 8)
提问by hockeyman
What I've done:
我所做的:
I have a module with
我有一个模块
myfield = fields.Many2one('res.partner', string="Graduate", domain=[('is_graduated', '=', True)])
Then I have another class with
然后我有另一堂课
_inherit = 'res.partner'
is_graduated = fields.Boolean("Graduated before?", default=False)
graduations = fields.Many2many('my_module.courses', string="Graduation courses")
What I get:
我得到的:
The myfield
works good, but the graduations
field is empty. If you edit user 1
profile you can add entries to graduation
field using Add item
, but I need it to be filled automaticaly.
该myfield
作品不错,但graduations
字段为空。如果您编辑user 1
个人资料,您可以使用 将条目添加到graduation
字段中Add item
,但我需要自动填充它。
What I expect:
我的期望:
I expect that every record where myfield
is set to lets say user 1
, will be visible in field graduations
when you open user 1
profile. When I create record and set myfield
value to lets say user 1
, that record must to be visible in user 1
profile in the field graduations
. How to achieve that?
我希望当您打开个人资料时,每个myfield
设置为的记录user 1
都将在字段中可见。当我创建记录并将值设置为可以说时,该记录必须在字段中的配置文件中可见。如何做到这一点?graduations
user 1
myfield
user 1
user 1
graduations
采纳答案by Sheliya Infotech
user_rel_ids = fields.Many2many(comodel_name='course', relation='user_course_rel', column1='user_id', column2='course_id')
user_rel_ids = fields.Many2many(comodel_name='course',relation='user_course_rel', column1='user_id', column2='course_id')
Or
或者
user_rel_id = fields.Many2many('course')
For Filling Data (for add new relation)
用于填充数据(用于添加新关系)
user_rel_id = [(4,course_id)]
According to http://odoo4u.blogspot.com/2014/10/orm-methods.html, It says: A full list of options is in the documentation for the class. This same thing will apply for one2many
根据http://odoo4u.blogspot.com/2014/10/orm-methods.html,它说:该类的文档中有完整的选项列表。同样的事情将适用于 one2many
For a many2manyand one2manyfield, 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 recordthat needs to be created with the given values dictionary
(1, ID, { values })
updatethe linked record with id = ID (write values on it)
(2, ID)
remove and deletethe 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 relationshipbetween the two objects but does not delete the target object itself)
(4, ID)
linkto existing record with id = ID (adds a relationship)
(5)
unlink all(like using (3, ID) for all linked records)
(6, 0, [IDs])
replacethe list of linked IDs (like using (5) then (4,ID) for each ID in the list of IDs)
对于many2many和one2many字段,需要一个元组列表。这是被接受的元组列表,具有相应的语义:
(0, 0, { values })
链接到需要使用给定值字典创建的新记录
(1, ID, { values })
使用 id = ID更新链接记录(在其上写入值)
(2, ID)
删除并删除带有 id = ID 的链接记录(在 ID 上调用 unlink,这将完全删除对象,以及指向它的链接)
(3, ID)
将id = ID的链接记录切断链接(删除两个对象之间的关系但不删除目标对象本身)
(4, ID)
链接到 id = ID 的现有记录(添加关系)
(5)
取消所有链接(例如对所有链接记录使用 (3, ID))
(6, 0, [IDs])
替换链接的 ID 列表(例如对 ID 列表中的每个 ID 使用 (5) 然后 (4,ID))
回答by Juan Salcedo
You need to use an onchange method
for myfield
, then inside it you need to fill the graduations
field, something like this:
您需要使用onchange method
for myfield
,然后在其中填写该graduations
字段,如下所示:
@api.onchange('myfield'):
def _onchange_myfield(self):
#fill graduations field here...
...
回答by Bhavesh Odedra
You can achieve like these.
你可以实现这些。
For example:
例如:
@api.one
@api.depends(
#here you may define your depend field name
)
def _set_graduations(self):
#here comes your logic which will collect ids
#and than return it with self.field_name like
self.graduations = [list_of_ids]
graduations = fields.Many2many('my_module.courses', string='Payments',
compute='_set_graduations')
If you don't want to use @api.dependsthan you may use @api.multi. For reference you may check out accountmodule with account_invoice.pyfile.
如果您不想使用@api.depends,则可以使用@api.multi。作为参考,您可以使用account_invoice.py文件查看帐户模块。
回答by Bhautik
_inherit = 'crm.phonecall'
alarm_ids = fields.Many2many('calendar.alarm',string="Reminders")
set the alarm_ids of calendar.event model in create method of crm phonecall...
alarm_ids = [(6,0,self.alarm_ids.ids)]
_inherit = 'calendar.event'
alarm_ids = fields.Many2many('calendar.alarm',string="Reminders")