Odoo Help

Welcome!

This community 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.

1

How can I mark a field "unique" in a custom model?

By
Martin
on 6/26/13, 3:32 PM 5,759 views

When I create a custom model in the web GUI, how can I mark a field as "unique", so that the database will never create a second row with the same field value?

5

Ray Carnes

--Ray Carnes--
15717
| 9 7 9
Greater Los Angeles, United States
--Ray Carnes--

Senior Odoo Analyst

OpenERP 6.1, 7.0 and Odoo 8.0, 9.0 (Since 2012)

Completed Functional and Technical Training.

Major Skills:

  • Needs Discovery and Requirements Analysis;

  • Function and Technical Specifications;

  • Project Planning;

  • Prototyping and Proof of concepts;

  • Data migration;

  • Configuration & Customization (UI and modules);

  • Integration - data, business logic and service levels;

  • Training and Knowledge transfer;

  • Go Live support;

  • Help desk;

  • Version Migration.

I have over 20 years of experience empowering and enabling users with enterprise information systems that make a real and measurable difference in their ability to proactively manage their businesses and organizations. 

Ray Carnes
On 6/26/13, 4:42 PM

You can only do this when you create your field via Python.

See https://accounts.openerp.com/forum/Help-1/question/16386

_sql_constraints = [
                     ('field_unique', 
                      'unique(field)',
                      'Choose another value - it has to be unique!')
]

Note that if there are existing records for the model in the database, then any field values have to be unique, or this will fail and OpenERP will do nothing to enforce the constraint for new records.

Thanks, so I will have to write a Python module for my table. Maybe it would be a nice enhancement if there would be a "unique" flag in the GUI as well. I assume, that unique fields are needed often (serial numbers, order numbers, etc.).

Martin
on 6/26/13, 7:58 PM
1
Thierry Godin
On 6/26/13, 4:40 PM

Hi,

add a sql constraint to your model :

_sql_constraints = [
        ('uniq_name', 'unique(stuff_name)', "A stuff already exists with this name . Stuff's name must be unique!"),
    ]

Best regards

Thanks, too!

Martin
on 6/26/13, 8:00 PM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

0 follower(s)

Stats

Asked: 6/26/13, 3:32 PM
Seen: 5759 times
Last updated: 3/16/15, 8:10 AM