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账号

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

    【分享】仅限制公司名唯一

    Odoo 开发与实施交流
    6
    9
    5539
    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.
    • Y
      youring last edited by

      网上找得到的都是限制合作伙伴partner名称唯一的,V7.0联系人和公司放在一张表里,如果限制partner名唯一,就无法创建同名联系人了。
      搜源码,照着addons\base\res\res_currency.py, 拼凑了一下,测试居然成功了 😉

      主要代码,不当之处,恳请斧正:

      # -*- coding: utf-8 -*-<br /><br />from openerp.osv import fields, osv<br /><br />class res_partner(osv.osv):<br />&nbsp; &nbsp; _inherit = &#039;res.partner&#039;<br /><br />&nbsp; &nbsp; _sql_constraints = [<br />&nbsp; &nbsp; &nbsp; &nbsp; # Empty constraint, complemented by unique index (see below)<br />&nbsp; &nbsp; &nbsp; &nbsp; # Still useful to keep sql constraints because it provides a proper error message when a violation occurs.<br />&nbsp; &nbsp; &nbsp; &nbsp; # Because the constraint and index share the same prefix so that IntegrityError triggered by the index will be caught<br />&nbsp; &nbsp; &nbsp; &nbsp; # and reported to the user with the sql constraint&#039;s error message.<br />&nbsp; &nbsp; &nbsp; &nbsp; (&#039;name_is_company_unique&#039;, &#039;unique ()&#039;, &#039;Company names must be unique&#039;),<br />&nbsp; &nbsp; ]<br /><br />&nbsp; &nbsp; def init(self, cr, context=None):<br />&nbsp; &nbsp; &nbsp; &nbsp; super(res_partner, self)._auto_init(cr, context)<br />&nbsp; &nbsp; &nbsp; &nbsp; cr.execute(&quot;&quot;&quot;SELECT indexname FROM pg_indexes WHERE indexname = &#039;res_partner_name_is_company_unique_idx&#039;&quot;&quot;&quot;)<br />&nbsp; &nbsp; &nbsp; &nbsp; if not cr.fetchone():<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cr.execute(&quot;&quot;&quot;CREATE UNIQUE INDEX &quot;res_partner_name_is_company_unique_idx&quot; ON res_partner (name) WHERE is_company IS True&quot;&quot;&quot;)<br /><br />res_partner()
      



      两张图解释以上代码中的注释部分:
      [attach=1]  [attach=2]

      下载:<br / https://github.com/youring/company_unique br />OpenERP 7.0 装完此模块后,创建合作伙伴Partner时,如果选中“是一个公司”  (is_company = true),则无法创建同名partner,即公司名要唯一;
      如果取消选中“是一个公司” ,则可以创建同名partner,即联系人可以同名,比如可以创建n个张先生。

      1 Reply Last reply Reply Quote 0
      • mrshelly
        mrshelly last edited by

        unique (name, is_company) 应该就可以了呀?

        init  的目的是啥?

        赞一个...

        1 Reply Last reply Reply Quote 0
        • Y
          youring last edited by

          unique (name, is_company) 不仅限制了公司同名,也限制了联系人同名。
          而实际上,公司内或公司之间,联系人同名的情况很常见。

          紧靠sql constraint无法实现上述要求,见
          \addons\base\res\res_currency.py

          &nbsp; &nbsp; def init(self, cr):<br />&nbsp; &nbsp; &nbsp; &nbsp; # CONSTRAINT/UNIQUE INDEX on (name,company_id) <br />&nbsp; &nbsp; &nbsp; &nbsp; # /!\ The unique constraint &#039;unique_name_company_id&#039; is not sufficient, because SQL92<br />&nbsp; &nbsp; &nbsp; &nbsp; # only support field names in constraint definitions, and we need a function here:<br />&nbsp; &nbsp; &nbsp; &nbsp; # we need to .....
          
          1 Reply Last reply Reply Quote 0
          • 卓忆
            卓忆 last edited by

            谢谢分享,先mark了,
            我还要不让产品名称 同名,

            1 Reply Last reply Reply Quote 0
            • A
              alanljj last edited by

              这个好用,支持一下!

              1 Reply Last reply Reply Quote 0
              • Z
                zamba last edited by

                有没有人尝试在 Odoo v9.0 中该解决该问题?

                求救中,,,

                1 Reply Last reply Reply Quote 0
                • D
                  dwz35296 last edited by

                  记录一下 😉

                  1 Reply Last reply Reply Quote 0
                  • mrshelly
                    mrshelly last edited by

                    哪辈年的贴子了,不过还能滑灰作用,太强大了

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