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

原论坛用户的基本信息和发帖这里都予以保留,请注意:原论坛用户无需重新注册新用户,但是您的密码需要重置

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

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

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



  • 网上找得到的都是限制合作伙伴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个张先生。



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

    init  的目的是啥?

    赞一个...



  • 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 .....
    


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



  • 这个好用,支持一下!



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

    求救中,,,



  • 记录一下 😉



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