跳转至内容
  • 版块
  • 标签
  • 热门
  • 用户
  • 群组
皮肤
  • 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. On2many字段如何隐藏New button或者只允许建立一条记录

On2many字段如何隐藏New button或者只允许建立一条记录

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

    系统中,res.partner和res.partner.address是one2many的关系,在客户界面,可以新建任意数量的address记录
    我现在有一个自己的class,和res.partner也是one2many的关系,但是现在我想隐层掉界面中的New和delete按钮,也就是只允许建立一条记录。
    在网上搜索了一下,有如下的方法可以只建立一条记录,但是无法隐层new按钮:
    _sql_constraints = [
            ('one_address_per_partner', 'UNIQUE(partner_id)', 'You can only create one address per partner'),
        ]
    但是我增加了上面的代码,却得不到任何反应,也就是说仍然可以新建记录。
    请问是否有其他更好的办法了直接隐藏按钮的?
    谢谢!

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

      在群里咨询此问题,得到mrshelly指点试用related解决上述问题,不过经过考虑,还是有一些疑问,写出来求教。
      自定义类:

      <br />class res_partner_base_info(osv.osv):<br />&nbsp; &nbsp; _description =&#039;Partner Information&#039;<br />&nbsp; &nbsp; _name = &#039;res.partner.base.info&#039;<br />&nbsp; &nbsp; _order = &#039;name&#039;<br />&nbsp; &nbsp; _columns = {<br />&nbsp; &nbsp; &nbsp; &nbsp; &#039;partner_id&#039;: fields.many2one(&#039;res.partner&#039;, &#039;Partner Name&#039;, ondelete=&#039;set null&#039;, select=True),<br />&nbsp; &nbsp; &nbsp; &nbsp; &#039;name&#039;: fields.char(&#039;Name&#039;, size=64, select=1),<br />&nbsp; &nbsp; &nbsp; &nbsp; &#039;partner_type&#039;: fields.many2one(&#039;res.partner.type&#039;,&#039;企业类别&#039;),&nbsp; &nbsp; &nbsp;  <br />&nbsp; &nbsp; &nbsp; &nbsp; &#039;partner_source&#039;: fields.many2one(&#039;res.partner.source&#039;,&#039;客户来源&#039;),&nbsp; <br />&nbsp; &nbsp; &nbsp; &nbsp; &#039;partner_brief&#039;: fields.text(&#039;客户简介&#039;),<br />&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; <br />res_partner_base_info()<br />
      


      在res.partner中关联此类:

      <br />class res_partner(osv.osv):&nbsp; &nbsp; <br />&nbsp; &nbsp; _name = &quot;res.partner&quot;<br />&nbsp; &nbsp; _inherit = &#039;res.partner&#039;<br />&nbsp; &nbsp; _columns = {&nbsp; &nbsp; &nbsp;  <br />&nbsp; &nbsp; &nbsp; &nbsp; &#039;base_info&#039;: fields.one2many(&#039;res.partner.base.info&#039;, &#039;partner_id&#039;, &#039;Base&#039;),&nbsp; &nbsp; &nbsp;  <br />&nbsp; &nbsp; }<br />res_partner()<br />
      


      在crm.lead中关联此类:

      <br />class crm_lead(osv.osv):&nbsp; &nbsp; <br />&nbsp; &nbsp; _name = &quot;crm.lead&quot;<br />&nbsp; &nbsp; _inherit = &#039;crm.lead&#039;<br />&nbsp; &nbsp; _columns = {&nbsp; &nbsp; &nbsp;  <br />&nbsp; &nbsp; &nbsp; &nbsp; &#039;base_info&#039;: fields.one2many(&#039;res.partner.base.info&#039;, &#039;partner_id&#039;, &#039;Base&#039;),&nbsp; &nbsp; &nbsp;  <br />&nbsp; &nbsp; }<br />crm_lead()<br />
      


      在partner和lead的view中可以使用同样的界面定义:

      <br />&lt;field colspan=&quot;4&quot; mode=&quot;form&quot; name=&quot;base_info&quot; nolabel=&quot;1&quot; select=&quot;1&quot; height=&quot;260&quot;&gt;<br />&nbsp; &nbsp; &lt;form string=&quot;Base Info&quot;&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &lt;!--base info的各个字段--&gt;<br />&nbsp; &nbsp; &lt;/form&gt;<br />&lt;/field&gt;<br />
      


      这样做的目的就是在销售过程中,在lead中修改的base info的信息与partner的base info信息是同步的,不论销售员在哪里修改均可。

      如果使用related实现上述功能的话,我所知的写法如下:

      <br />class res_partner(osv.osv):&nbsp; &nbsp; <br />&nbsp; &nbsp; _name = &quot;res.partner&quot;<br />&nbsp; &nbsp; _inherit = &#039;res.partner&#039;<br />&nbsp; &nbsp; _columns = {&nbsp; &nbsp; &nbsp;  <br />&nbsp; &nbsp; &nbsp; &nbsp; &#039;base_info_id&#039;: fields.many2one(&#039;res.partner.base.info&#039;,&#039;Base&#039;),<br />&nbsp; &nbsp; &nbsp; &nbsp; &#039;partner_type&#039;: fields.related(&#039;base_info_id&#039;, &#039;partner_type&#039;, type=&#039;many2one&#039;, string=&#039;Partner Type&#039;),&nbsp; &nbsp; <br />&nbsp; &nbsp; &nbsp; &nbsp; &#039;partner_brief&#039;: fields.related(&#039;base_info_id&#039;, &#039;partner_brief&#039;, type=&#039;text&#039;, string=&#039;Partner Brief&#039;),&nbsp; <br />&nbsp; &nbsp; &nbsp; &nbsp; …………#列出res.partner.base.info的所有字段<br />&nbsp; &nbsp; }<br />res_partner()<br />
      


      上述的写法与使用one2many相比是不是过于复杂了?
      在之前我是使用另外的方法实现同样的功能,即base info所有的字段都是定义在res.partner中,而在crm.lead定义相同名称的字段,当lead保存时,重写write函数,将lead中修改过的字段内容update到res.partner中。这样写一开始出了一个问题,因为没有考虑好update的条件,所以某些情况下,lead保存的时候将partner中原有的纪录覆盖掉了。
      所以我希望是不论在lead还是在partner中,都操作同一个base info纪录,这样无论如何也不会出错了,如果其他的模块需要调用partner info,那么也可以同样使用base info。
      所以使用related是否有更简洁有效的写法呢?
      非常感谢!

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

        通过google找到了一篇帖子,http://www.openerp.com/forum/topic29212.html,其中的示意图完全就是我想要实现的功能,如下:
        [attach=1]
        我按照其中的代码进行了尝试,不过遇到了几个问题:
        1、我只能在view中使用notas_base_id,这样才能在view中显示notas base其中的字段。不过问题就是这些字段无法保存数据
        2、view中使用notas_base_id,由于它还是one2many字段,所以,还是默认显示了那个New按钮,与文中所说的实现one2one的功能不太相符。
        请问如果采用如下的代码,上述的两个问题如何才能解决呢?
        非常感谢!

        <br />class notas_rel(osv.osv):<br />&nbsp; &nbsp; _name = &#039;notas.rel&#039;<br />&nbsp; &nbsp; _columns = {<br />&nbsp; &nbsp; &nbsp; &nbsp; &#039;notas_id&#039;: fields.one2many(&#039;notas.base&#039;,&#039;notas_id&#039;,&#039;Notas&#039;),<br />&nbsp; &nbsp; }<br /><br />class notas_base(osv.osv):<br />&nbsp; &nbsp; _name = &#039;notas.base&#039;<br />&nbsp; &nbsp; _columns = {<br />&nbsp; &nbsp; &nbsp; &nbsp; &#039;name&#039;: fields.char(&#039;Notas&#039;,size=128),<br />&nbsp; &nbsp; &nbsp; &nbsp; &#039;notas_id&#039;: fields.many2one(&#039;notas.rel&#039;,&#039;Notas&#039;),<br />&nbsp; &nbsp; }<br /><br />class my_class(osv.osv):<br />&nbsp; &nbsp; _name = &#039;my.class&#039;<br />&nbsp; &nbsp; _columns = {<br />&nbsp; &nbsp; &nbsp; &nbsp; ...<br />&nbsp; &nbsp; &nbsp; &nbsp; &#039;notas_rel_id&#039;: fields.many2one(&#039;notas.rel&#039;,&#039;Notas&#039;),<br />&nbsp; &nbsp; &nbsp; &nbsp; &#039;notas_base_id&#039;: fields.related(&#039;notas_rel_id&#039;,&#039;notas_id&#039;,type=&quot;one2many&quot;, relation=&quot;notas.base&quot;, string=&quot;Notas&quot;, store=False),<br />&nbsp; &nbsp; }<br />
        
        1 条回复 最后回复
        0
        • A 离线
          A 离线
          alangwansui
          写于 最后由 编辑
          #4

          既然值允许一个,为什么还要用one2many字段呢?

          1 条回复 最后回复
          0
          • O 离线
            O 离线
            oldrev
            写于 最后由 编辑
            #5

            [quote author=Melody link=topic=2871.msg9959#msg9959 date=1337911318]
            既然值允许一个,为什么还要用one2many字段呢?
            [/quote]

            这就是最好的解决办法

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

              不用one2many的话,可以用什么来达到我的目的呢?

              1 条回复 最后回复
              0

              • 登录

              • 没有帐号? 注册

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