Odoo v8 中的独特电子邮件字段

Unique email field in Odoo v8

我想将来自线索和联系人的电子邮件设置为唯一。

我尝试更改 /usr/lib/python2.7/dist-packages/openerp/models.py 文件的第 342 行,来自:

_sql_constraints = []

在:

_sql_constraints = [
        ('user_email', 'unique(user_email)', 'Please enter Unique Email'),
]

但是没有用。

什么是正确的做法,请给我一个完整的例子,因为我是Odoo的初学者,谢谢。

您更改了 sql 对 BaseModel 的约束。 系统中所有机型均以此机型为基础。所以这个改变的效果是给Odoo中的每一个模型添加约束,其中大部分甚至没有user_email字段。这样做的结果绝对是灾难性的。您的 Odoo 还没有失败的唯一原因是您没有使用升级选项,所以现在更改还没有传播到数据库。

首先立即还原更改。其次,您不应该直接更改 Odoo 源代码。一方面,如果您开始修改 Odoo 的源代码,您将永远无法将其更新到更新的版本(即使有安全更新),因为这会还原您的更改。

你应该做的是 create a new module 然后用它来扩展你想修改的模块:

class Lead(models.Model):
    _inherit = 'crm.lead'

    _sql_constraints = [
            ('user_email', 'unique(user_email)', 'Please enter Unique Email'),
    ]

注:a bug in early versions of Odoo 8 preventing one from changing sql constraints by extending an object. It is now fixed. Make sure you are using the most recent version of Odoo from git. If this is not possible it may be necessary to use a work around.