Odoo 中文社区

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

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

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

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

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

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

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

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

      网上找得到的都是限制合作伙伴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 条回复 最后回复 回复 引用 0
      • mrshelly
        mrshelly 最后由 编辑

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

        init  的目的是啥?

        赞一个...

        1 条回复 最后回复 回复 引用 0
        • Y
          youring 最后由 编辑

          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 条回复 最后回复 回复 引用 0
          • 卓忆
            卓忆 最后由 编辑

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

            恬淡

            1 条回复 最后回复 回复 引用 0
            • A
              alanljj 最后由 编辑

              这个好用,支持一下!

              1 条回复 最后回复 回复 引用 0
              • Z
                zamba 最后由 编辑

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

                求救中,,,

                1 条回复 最后回复 回复 引用 0
                • D
                  dwz35296 最后由 编辑

                  记录一下 😉

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

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

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