3

I want to set the email from Leads and Contacts unique.

I have tried to change in /usr/lib/python2.7/dist-packages/openerp/models.py file, line 342 from:

_sql_constraints = []

in:

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

But is not working.

What is the right approach, please give me a full example, because I am a beginner in Odoo, thank you.

radu c
  • 4,138
  • 7
  • 30
  • 45

1 Answers1

8

You changed sql constraints on the BaseModel. All models in the system are based on this model. So the effect of this change is to add the constrain to every model in Odoo, most of which doesn't even have user_email field. The result of this could be absolutely disastrous. The only reason your Odoo haven't failed yet is you didn't use the upgrade option, so for now the changes haven't propagate to the database.

First of all revert the changes immediately. Second of all you are not supposed to ever change Odoo source code directly. For one thing, if you start modifying Odoo's source code you will never be able to update it to a newer version (even with a security update), since this would revert your changes.

What you should do instead is to create a new module and then use it to extend the module you want to modify:

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

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

Note: There was 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.

Ludwik Trammer
  • 24,602
  • 6
  • 66
  • 90
  • OK, thank you for corrections. I have made the module and still not working after I have add the class in `addons/mymodule/models.py` the version of Odoo is : Odoo Server 8.0-20150916. Do I need to restart service or something? – radu c Sep 20 '15 at 18:15
  • You need to create an `__openerp__.py` manifest file for you module, and an `__init__.py` file referencing your models.py file. Then you need to install the module. I strongly recommend reading the article about creating modules: https://www.odoo.com/documentation/8.0/howtos/backend.html – Ludwik Trammer Sep 20 '15 at 18:19
  • I have generated the module as described `odoo.py scaffold mymodule addons` and changed the `__openerp__.py` file and added the code for unique email field in `models.py`. How can I install the module, I should see it in `Settings>Modules>Apps` ? – radu c Sep 20 '15 at 18:39
  • Ok, that's very good. You should be able to see it after [updating the modules list](http://stackoverflow.com/a/27848408/262618). – Ludwik Trammer Sep 20 '15 at 18:41
  • Ok done, but still I can add emails that are not unique, the `_sql_constraints` in not good? – radu c Sep 20 '15 at 18:45
  • Looks good, but make sure there are no non-unique email records when you install your module. If there are any, Odoo will not be able to add the constraint to postgresql. – Ludwik Trammer Sep 20 '15 at 18:49
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/90173/discussion-between-ek-kosmos-and-ludwik-trammer). – radu c Sep 20 '15 at 18:52