Odoo中文社区可以通过以下两个域名访问:shine-it.net , odoo.net.cn

由于系统升迁的原因,本论坛部分较早期的内容存在格式和链接损坏失效的问题,并非本论坛系统本身的缺陷,望谅解

本社区没有维护任何QQ群讨论组,任何与本社区同名的QQ群讨论组的言论与本社区无关!

开发人员可以登录gitter讨论组: http://gitter.im/odoo-china/Talk, 需要github账号

如果您登录系统碰到问题,请在微信公众号留言:

M2m字段数值传递



  • 如下面的代码,我在lead中创建了一个m2m的字段,在创建partner时,需要将此字段的值传递到partner的同名字段下,请问应该如何做呢?
    'categ_sa_id':lead.categ_sa_id这样代码没有任何作用。
    非常感谢!

    <br />class res_partner_categ_sales_area(osv.osv):<br />&nbsp; &nbsp; _name = &#039;res.partner.categ.sales.area&#039;<br />&nbsp; &nbsp; _columns = {<br />&nbsp; &nbsp; &nbsp; &nbsp; &#039;name&#039;: fields.char(&#039;Sales Area&#039;, required=True, size=46, translate=True),&nbsp; &nbsp; &nbsp; <br />&nbsp; &nbsp; }<br />&nbsp; &nbsp; _order = &#039;name&#039;<br />res_partner_categ_sales_area()<br /><br />class crm_lead(osv.osv):<br />&nbsp; &nbsp; _name = &quot;crm.lead&quot;<br />&nbsp; &nbsp; _inherit = &#91;&#039;crm.lead&#039;]<br />&nbsp; &nbsp; _columns = {<br />&nbsp; &nbsp; &nbsp; &nbsp; &#039;categ_sa_id&#039;: fields.many2many(&#039;res.partner.categ.sales.area&#039;, &#039;lead_sa_rel&#039;, &#039;lead_id&#039;, &#039;categ_sa_id&#039;, &#039;Categories&#039;),<br />}<br /><br />&nbsp; &nbsp; def _lead_create_partner(self, cr, uid, lead, context=None):<br />&nbsp; &nbsp; &nbsp; &nbsp; partner = self.pool.get(&#039;res.partner&#039;)<br />&nbsp; &nbsp; &nbsp; &nbsp; partner_id = partner.create(cr, uid, {<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  #other fields<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  &#039;categ_sa_id&#039;:lead.categ_sa_id,&nbsp; #这行没有任何作用<br />&nbsp; &nbsp; &nbsp; &nbsp; })<br />&nbsp; &nbsp; &nbsp; &nbsp; return partner_id<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  <br />crm_lead()<br /><br />class res_partner(osv.osv):<br />&nbsp; &nbsp; _name = &quot;res.partner&quot;<br />&nbsp; &nbsp; _inherit = &#039;res.partner&#039;<br />&nbsp; &nbsp; _columns = {<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#039;categ_sa_id&#039;: fields.many2many(&#039;res.partner.categ.sales.area&#039;, &#039;partner_sa_rel&#039;, &#039;partner_id&#039;, &#039;categ_sa_id&#039;, &#039;Categories&#039;),<br />}<br />res_partner()<br />
    


  • 试试这个

    <br />#假设lead 是 osv object<br />&#039;categ_sa_id&#039;: [ (6, 0, [o.id for o in lead.categ_sa_id])]&nbsp; ,&nbsp; #试试这个<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]



  • 这个example这样代码是什么意思呢?似乎看着不像Python代码。



  • For a many2many field, a list of tuples is expected.
    对于many2many 字段,需要提供一个 由 tuples 组成的 list

    您的代码中 lead.categ_sa_id 这里应该是一个 osv object ,所以
    'categ_sa_id': [(6, 0, [o.id for o in lead.categ_sa_id])],

    试试吧  😃



  • 非常感谢,我晚上回去试试再来报告



  • 没有什么可以表达我的感激之情了。
    这行代码完全可以满足需要。
    谢谢wangbuke,非常感谢。


  • 管理员

    以前写了个博客关于这个的: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删除原来的记录。



  • 谢谢Joshua,我现在终于理解了这段说明的含义了,非常感谢



  • 这个看来是难点之一啊

    经常有人问


Log in to reply