Odoo Help


How can I enforce fields to only allow unique values?

on 7/31/14, 10:28 PM 1,219 views

bit of a newbie here so sorry if this question isn't good.

There are many fields in odoo that I would like to enforce unique data - eg Product Name, Category, Internal Ref etc...may other fields and modules.

Do I have to modify all the python object modules throughout odoo with SQL Contraints????  I was expecting just to check a box against a field to make it unique!

If I do have to add a python SQL constraints in python modules am I best to modify the odoo modules, or should I inherit the standard odoo object into a new object with my added constraints thereby keeping the odoo module un-modified?  eg if a standard odoo module is updated then my added constraints will still apply?

Hope this is a sensible question!





On 4/21/17, 8:41 AM

An example to use check whether data is unique in a field.

    _sql_constraints = [

        ('field_name', 'unique (field_name)', 'The field  must be unique  !')



Ludo - Neobis

--Ludo - Neobis--
| 2 2 5
Hoogvliet, Netherlands
--Ludo - Neobis--


Ludo - Neobis
On 8/1/14, 3:19 AM

Yes, you must unfortunately bring in custom code and (SQL) constraints for this. That is nearly always a given.

Due to the modular setup of Odoo the best way to modify code is as you suggested in your second statement, by using the way of inheriting models and adding/removing code in your own custom module. It is really simple to do, so you should have no trouble doing so.

Example for "old" (read: current) ORM is like so:

class my_partner(osv.osv)

    _name = "res.partner"

    _inherit = "res.partner"

    <<SQL constraint here>>

About This Community

This platform is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.


Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

1 follower(s)


Asked: 7/31/14, 10:28 PM
Seen: 1219 times
Last updated: 4/21/17, 8:41 AM