This question has been flagged
3 Replies
7022 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 ?

Avatar
Discard
Author Best Answer

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

Avatar
Discard

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

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

Best Answer

Hi,

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(
            'product.variant',
            '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.

Avatar
Discard

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.

Author

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 ?

Author

"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 :)?

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.

Author

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.

Author

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 :)