跳转至内容
  • 版块
  • 标签
  • 热门
  • 用户
  • 群组
皮肤
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • 默认(Flatly)
  • 不使用皮肤
折叠

Odoo 中文社区

  1. 主页
  2. 版块
  3. Odoo 开发与实施交流
  4. 用context控制many2one字段显示内容的问题

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

已定时 已固定 已锁定 已移动 Odoo 开发与实施交流
4 帖子 2 发布者 5.3k 浏览
  • 从旧到新
  • 从新到旧
  • 最多赞同
登录后回复
此主题已被删除。只有拥有主题管理权限的用户可以查看。
  • J 离线
    J 离线
    jerry79
    写于 最后由 编辑
    #1

    定义了一个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
    • JoshuaJ 离线
      JoshuaJ 离线
      Joshua 管理员
      写于 最后由 编辑
      #2

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

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

      1 条回复 最后回复
      0
      • J 离线
        J 离线
        jerry79
        写于 最后由 编辑
        #3

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

        1 条回复 最后回复
        0

        • 登录

        • 没有帐号? 注册

        • 登录或注册以进行搜索。
        • 第一个帖子
          最后一个帖子
        0
        • 版块
        • 标签
        • 热门
        • 用户
        • 群组