M2m字段数值传递
-
如下面的代码,我在lead中创建了一个m2m的字段,在创建partner时,需要将此字段的值传递到partner的同名字段下,请问应该如何做呢?
'categ_sa_id':lead.categ_sa_id这样代码没有任何作用。
非常感谢!<br />class res_partner_categ_sales_area(osv.osv):<br /> _name = 'res.partner.categ.sales.area'<br /> _columns = {<br /> 'name': fields.char('Sales Area', required=True, size=46, translate=True), <br /> }<br /> _order = 'name'<br />res_partner_categ_sales_area()<br /><br />class crm_lead(osv.osv):<br /> _name = "crm.lead"<br /> _inherit = ['crm.lead']<br /> _columns = {<br /> 'categ_sa_id': fields.many2many('res.partner.categ.sales.area', 'lead_sa_rel', 'lead_id', 'categ_sa_id', 'Categories'),<br />}<br /><br /> def _lead_create_partner(self, cr, uid, lead, context=None):<br /> partner = self.pool.get('res.partner')<br /> partner_id = partner.create(cr, uid, {<br /> #other fields<br /> 'categ_sa_id':lead.categ_sa_id, #这行没有任何作用<br /> })<br /> return partner_id<br /> <br />crm_lead()<br /><br />class res_partner(osv.osv):<br /> _name = "res.partner"<br /> _inherit = 'res.partner'<br /> _columns = {<br /> 'categ_sa_id': fields.many2many('res.partner.categ.sales.area', 'partner_sa_rel', 'partner_id', 'categ_sa_id', 'Categories'),<br />}<br />res_partner()<br />
-
试试这个
<br />#假设lead 是 osv object<br />'categ_sa_id': [ (6, 0, [o.id for o in lead.categ_sa_id])] , #试试这个<br />
参考 ~/openerp/openerp/osv/orm.py 中 write () 方法 第3807行
+ For a many2many field, a list of tuples is expected.
......
Example:
[(6, 0, [8, 5, 6, 4])] sets the many2many to ids [8, 5, 6, 4] -
非常感谢,我找到这段代码注释了,但是真的是看不懂啊。
For a many2many field, a list of tuples is expected. Here is the list of tuple that are 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)
Example:
[(6, 0, [8, 5, 6, 4])] sets the many2many to ids [8, 5, 6, 4] -
以前写了个博客关于这个的:http://cn.openerp.cn/289 ,楼主也可以参考下
many2many
(0,0,{values}) 根据values里面的信息新建一个记录。
(1,ID,{values})更新id=ID的记录(写入values里面的数据)
(2,ID) 删除id=ID的数据(调用unlink方法,删除数据以及整个主从数据链接关系)
(3,ID) 切断主从数据的链接关系但是不删除这个数据
(4,ID) 为id=ID的数据添加主从链接关系。
(5) 删除所有的从数据的链接关系就是向所有的从数据调用(3,ID)
(6,0,[IDs]) 用IDs里面的记录替换原来的记录(就是先执行(5)再执行循环IDs执行(4,ID))
例子[(6, 0, [8, 5, 6, 4])] 设置 many2many to ids [8, 5, 6, 4]
one2many
(0, 0,{ values })根据values里面的信息新建一个记录。
(1,ID,{values}) 更新id=ID的记录(对id=ID的执行write 写入values里面的数据)
(2,ID) 删除id=ID的数据(调用unlink方法,删除数据以及整个主从数据链接关系)
例子:
[(0,0,{'field_name':field_value_record1,...}),(0,0,{'field_name':field_value_record})]
many2one的字段比较简单,直接填入已经存在的数据的id或者填入False删除原来的记录。