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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 10:39:39  来源:igfitidea点击:

Filling Many2many field (odoo 8)

pythonxmlpostgresqlopenerpodoo

提问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 myfieldworks good, but the graduationsfield is empty. If you edit user 1profile you can add entries to graduationfield 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 myfieldis set to lets say user 1, will be visible in field graduationswhen you open user 1profile. When I create record and set myfieldvalue to lets say user 1, that record must to be visible in user 1profile in the field graduations. How to achieve that?

我希望当您打开个人资料时,每个myfield设置为的记录user 1都将在字段中可见。当我创建记录并将值设置为可以说时,该记录必须在字段中的配置文件中可见。如何做到这一点?graduationsuser 1myfielduser 1user 1graduations

采纳答案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)

对于many2manyone2many字段,需要一个元组列表。这是被接受的元组列表,具有相应的语义:

(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 methodfor myfield, then inside it you need to fill the graduationsfield, something like this:

您需要使用onchange methodfor 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")