填充 Many2many 字段 (odoo 8)
Filling Many2many field (odoo 8)
我做了什么:
我有一个模块
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
配置文件,您可以使用 Add item
将条目添加到 graduation
字段,但我需要它自动填充。
我的期望:
我希望当您打开 user 1
配置文件时,myfield
设置为 user 1
的每条记录都将在字段 graduations
中可见。当我创建记录并将 myfield
值设置为 user 1
时,该记录必须在字段 graduations
的 user 1
配置文件中可见。如何实现?
您需要为 myfield
使用 onchange method
,然后您需要在其中填写 graduations
字段,如下所示:
@api.onchange('myfield'):
def _onchange_myfield(self):
#fill graduations field here...
...
你可以这样实现。
例如:
@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')
如果您不想使用 @api.depends,您可以使用 @api.multi。作为参考,您可以查看 account 模块和 account_invoice.py 文件。
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,它说:
class 的文档中提供了完整的选项列表。
同样的事情将适用于 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)
_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")
我做了什么:
我有一个模块
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
配置文件,您可以使用 Add item
将条目添加到 graduation
字段,但我需要它自动填充。
我的期望:
我希望当您打开 user 1
配置文件时,myfield
设置为 user 1
的每条记录都将在字段 graduations
中可见。当我创建记录并将 myfield
值设置为 user 1
时,该记录必须在字段 graduations
的 user 1
配置文件中可见。如何实现?
您需要为 myfield
使用 onchange method
,然后您需要在其中填写 graduations
字段,如下所示:
@api.onchange('myfield'):
def _onchange_myfield(self):
#fill graduations field here...
...
你可以这样实现。
例如:
@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')
如果您不想使用 @api.depends,您可以使用 @api.multi。作为参考,您可以查看 account 模块和 account_invoice.py 文件。
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,它说: class 的文档中提供了完整的选项列表。 同样的事情将适用于 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)
_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")