Filling Many2many field (odoo 8)

依然范特西╮ 提交于 2019-11-27 11:50:29
Sheliya Infotech

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

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)

You need to use an onchange method for myfield, then inside it you need to fill the graduations field, something like this:

@api.onchange('myfield'):
def _onchange_myfield(self):
    #fill graduations field here...
    ...
_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") 

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.depends than you may use @api.multi. For reference you may check out account module with account_invoice.py file.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!