Navigation

    Odoo 中文社区

    • Register
    • Login
    • Search
    • Categories
    • Tags
    • Popular
    • Users
    • Groups

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

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

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

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

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

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

    Odoo 开发与实施交流
    3
    7
    5177
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • J
      jerry79 last edited by

      系统中,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 Reply Last reply Reply Quote 0
      • J
        jerry79 last edited by

        在群里咨询此问题,得到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 Reply Last reply Reply Quote 0
        • J
          jerry79 last edited by

          通过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 Reply Last reply Reply Quote 0
          • A
            alangwansui last edited by

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

            1 Reply Last reply Reply Quote 0
            • O
              oldrev last edited by

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

              这就是最好的解决办法

              1 Reply Last reply Reply Quote 0
              • J
                jerry79 last edited by

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

                1 Reply Last reply Reply Quote 0
                • First post
                  Last post