Odoo Help


How can i check - is name already in database ?

Dr Obx
on 5/3/15, 5:37 PM 1,270 views

To avoid multiplication of product names how can i check is name already exist in database, if "yes" display message  and hold the record process.

Haw can I do it ?

Dr Obx
On 5/7/15, 6:10 PM

So you suggest that I may have a problem because product.template already contain variant field?

The "product_variant_id" field is defined in "product.template" model. So you should give that _sql_constraints in "product.template"

Akhil P Sivan
on 5/7/15, 10:25 PM

Hi, I have updated the answer by correcting your code. Please check it.

Akhil P Sivan
on 5/8/15, 5:57 AM

Akhil P Sivan

--Akhil P Sivan--

| 5 4 7
Kochi, India
--Akhil P Sivan--

Interested in exploring Odoo both technically & functionality wise.

email-id : akhilpsivan01@gmail.com

Akhil P Sivan
On 5/3/15, 10:45 PM


You can use an _sql_constraints for that, by inheriting the product.template object.

For eg:

class product_template(models.Model):

_inherit = 'product.template'

_sql_constraints = [

('name_unique', 'UNIQUE(name)', _('Duplicate Product Name!')),


Let me correct your code. Please try the following in your .py file:

from openerp import models, fields, api
class product_variant(models.Model):
    _name = 'product.variant'
    name = fields.Char('Variant')
    selector = fields.Selection([ ('Computer','Computer'), ('Processor','Processor'), ('Storage_device','Storage device'),
            ('Optical_drive','Optical drive'), ('RAM_module','RAM module'), ('Graphics_card','Graphics card'), ('Storage_controller','Storage controller'), ('Power_supply','Power supply'), ('Monitor','Monitor'), ('Motherboard','Motherboard'), ('Cable','Cable'), ('Case_part','Case part'), ('Peripheral','Peripheral'), ('Internal_components','Internal components')],'selector')
class product_template(models.Model):
    _inherit = 'product.template'
    product_variant_id = fields.Many2one(
            'Variant', help='Select or add a variant of the IBM or Lenovo product if not exist', ondelete='restrict')
    _sql_constraints = [('product_variant_id_uniq', 'unique(product_variant_id)', 'Already exist!')]

May I know why are you trying to create a model for variants, as product variants are already there.

Note that at the moment when you add this contraint there should be no duplicates present in the database. Otherwise setting the constraint will fail without much of a notification.

Vizucom Oy, Timo Talvitie, Vizucom Oy
on 5/4/15, 1:49 AM

Apparently I've tried to add code which is for new API into the old one. That's why it won't work. Right ? The code You showed me is new API. Because my module is "old API" how can i do it in old ? Or should I create another module just to add this _sql_constraints modification ?

Dr Obx
on 5/8/15, 9:06 AM

"May I know why are you trying to create a model for variants, as product variants are already there." Is not like that that one create a field in product_template and second is adding name and selector into the variant in product_template ? Maybe I just misunderstood something :)?

Dr Obx
on 5/8/15, 10:36 AM

No Robert, no need to create a seperate module just for that. I feel like, to work with new api is bit more easier. You can also use old api within the new api but reverse is not possible. If you need it in old api, no problem, I can help you with that.

Akhil P Sivan
on 5/8/15, 10:43 AM

To have a drop down list with option "Create/Add" i decided to do it that way. If you want check this film then you will see what is it for. https://www.youtube.com/watch?v=od2eNWU83B8 Product module contain a couple of drop down lists to automatically create based on the same details product name to keep same form and syntax. Because people quiet often naming product in theirs own way (we don't like it) I created something like this to make it easier. Now I'm only improving it.

Dr Obx
on 5/8/15, 10:47 AM

Oh thank you Akhil, it's very nice of you. To be honest I just started learning how to build modules, how to change things in Odoo so my knowledge is not ... very broad :)

Dr Obx
on 5/8/15, 10:52 AM

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: 5/3/15, 5:37 PM
Seen: 1270 times
Last updated: 6/8/15, 7:52 AM