跳转至内容
  • 版块
  • 标签
  • 热门
  • 用户
  • 群组
皮肤
  • 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. 【分享】仅限制公司名唯一

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

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

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

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

    # -*- 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
    • mrshellyM 离线
      mrshellyM 离线
      mrshelly
      写于 最后由 编辑
      #2

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

      init  的目的是啥?

      赞一个...

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

        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
        • 卓忆卓 离线
          卓忆卓 离线
          卓忆
          写于 最后由 编辑
          #4

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

          恬淡

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

            这个好用,支持一下!

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

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

              求救中,,,

              1 条回复 最后回复
              0
              • D 离线
                D 离线
                dwz35296
                写于 最后由 编辑
                #7

                记录一下 ;D

                1 条回复 最后回复
                0
                • mrshellyM 离线
                  mrshellyM 离线
                  mrshelly
                  写于 最后由 编辑
                  #8

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

                  1 条回复 最后回复
                  0

                  • 登录

                  • 没有帐号? 注册

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