Odoo 中文社区

    • 注册
    • 登录
    • 搜索
    • 版块
    • 标签
    • 热门
    • 用户
    • 群组

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

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

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

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

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

    用context控制many2one字段显示内容的问题

    Odoo 开发与实施交流
    2
    4
    4874
    正在加载更多帖子
    • 从旧到新
    • 从新到旧
    • 最多赞同
    回复
    • 在新帖中回复
    登录后回复
    此主题已被删除。只有拥有主题管理权限的用户可以查看。
    • J
      jerry79 最后由 编辑

      定义了一个m2o字段,如下

      <br /> #*.py<br /> &#039;partner_address_id&#039;: fields.many2one(&#039;res.partner.address&#039;, &#039;联系人&#039;,domain=&quot;[(&#039;partner_id&#039;,&#039;=&#039;,partner_id)]&quot;),<br /><br /> #*.xml<br />&lt;field name=&quot;partner_address_id&quot; context=&quot;{&#039;contact_display&#039;:&#039;name&#039;}&quot;/&gt;<br />
      



      然后重新了res_partner_address的name_get函数,代码如下:

      <br />def name_get(self, cr, user, ids, context=None):<br />&nbsp; &nbsp; &nbsp; &nbsp; if context is None:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; context = {}&nbsp; &nbsp; &nbsp; &nbsp; <br />&nbsp; &nbsp; &nbsp; &nbsp; if not len(ids):<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return &#91;]<br />&nbsp; &nbsp; &nbsp; &nbsp; res = &#91;]<br />&nbsp; &nbsp; &nbsp; &nbsp; for r in self.read(cr, user, ids, &#91;&#039;type_id&#039;,&#039;name&#039;,&#039;state_id&#039;,&#039;city&#039;,&#039;district&#039;,&#039;street&#039;,&#039;zip&#039;,&#039;partner_id&#039;]):<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if context.get(&#039;contact_display&#039;, &#039;contact&#039;)==&#039;partner&#039; and r&#91;&#039;partner_id&#039;]:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; res.append((r&#91;&#039;id&#039;], r&#91;&#039;partner_id&#039;][1]))<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ################增加的代码<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elif (context.get(&#039;contact_display&#039;,&#039;contact&#039;)==&#039;name&#039;) and r&#91;&#039;partner_id&#039;]:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; res.append((r&#91;&#039;id&#039;],r&#91;&#039;name&#039;] or &#039;/&#039;))<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ################<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # make a comma-separated list with the following non-empty elements<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elems = [r&#91;&#039;type_id&#039;] and r&#91;&#039;type_id&#039;][1],r&#91;&#039;name&#039;],r&#91;&#039;state_id&#039;] and r&#91;&#039;state_id&#039;][1], r&#91;&#039;city&#039;], r&#91;&#039;district&#039;],r&#91;&#039;street&#039;],r&#91;&#039;zip&#039;]]<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; addr = &#039;, &#039;.join(filter(bool, elems))<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (context.get(&#039;contact_display&#039;, &#039;contact&#039;)==&#039;partner_address&#039;) and r&#91;&#039;partner_id&#039;]:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; res.append((r&#91;&#039;id&#039;], &quot;%s: %s&quot; % (r&#91;&#039;partner_id&#039;][1], addr or &#039;/&#039;)))<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; res.append((r&#91;&#039;id&#039;], addr or &#039;/&#039;))<br />&nbsp; &nbsp; &nbsp; &nbsp; return res<br />
      


      我现在就是希望在选择地址的时候,可以在不同的form中有不同的选项,如本文一开始定义的form的address字段,我希望下拉列表中只显示联系人的名称。修改name_get字段后,选择的时候确实只有联系人的名称,即这样代码的输出:
          res.append((r['id'],r['name'] or '/'))
      但是当保存之后的显示内容却变成了这行代码的输出:
          res.append((r['id'], addr or '/'))

      看上去似乎,保存的时候context失效了,因此执行到了else语句里面,我用log追踪了一下,确实如此,请问为什么会这样呢?
      非常感谢!

      1 条回复 最后回复 回复 引用 0
      • Joshua
        Joshua 管理员 最后由 编辑

        尝试在这个视图的action 里面添加 context="{'contact_display':'name'}"

        【上海先安科技】(joshua AT openerp.cn),欢迎关注公众号:openerp_cn

        1 条回复 最后回复 回复 引用 0
        • J
          jerry79 最后由 编辑

          经过测试,在action那里加了context也不行。

          1 条回复 最后回复 回复 引用 0
          • First post
            Last post