Skip to Content
Menu
This question has been flagged
4 Replies
8035 Views

Hello,

I'm new to odoo and i'm trying to modify the 'add product' a little, more exactly the internal reference field. I would like to add a dynamic search to it, so that when i start to enter a reference, i get below a list of references that match what i have already entered. More exactly you can see this already implemented in Purchases - Requests for Quotation - Create - Add an item - product column.

I tried to start small and create a simple module to just have the internal reference unique. But i can't figure it out. I've read the few posts that mention this, but i couldn't make it work. My current code is:

class product_product(osv.osv):
    _name = "product.product"
_description = "Product"
_inherits = {'product.template': 'product_tmpl_id'}

_sql_constraints = [
('default_code', 'unique(default_code)', 'Product Reference must be unique!'),
]
product_product()

I also tried this module https://www.odoo.com/apps/modules/8.0/product_unique_default_code/ , but it doesn't work either.

If you have any idea please help. Also, i can't find any proper docs for odoo. Their website seems to only have bits and pieces. Do you know any better tutorials?

Thank you

Avatar
Discard
Author Best Answer

Thank you for the answer. Yes, i have already tried like that, too, and it didn't work.

-----

Thank you for the answers. Sorry, apparently i can't comment or post again so the only way to answer seems to edit my only post. First of all sorry for the delay, only now i had the chance to test again.

@Teemur, @Drees Far: if i use only "_inherit = ['product.template']"  then i get an error when trying to install the module: KeyError: 'product_tmpl_id'

@Emipro Technologies Pvt. Ltd.: i have already tried this, but it doesn't do anything. The module installs ok, but then i can add products with the same default_code.

---------

I finally got it to work. It was a combination between all the answers. So, the py code is:

class product_product(osv.osv):
_name = "product.product"
_description = "Product"
_inherit = 'product.product'
_columns = {
'default_code' : fields.char('Internal Reference', select=True),
}
_sql_constraints = [
('default_code', 'unique(default_code)', 'Product Reference must be unique!'),
]

product_product()

and the xml code is

<openerp>
<data>
<record id="view_product_form_inherit" model="ir.ui.view">
<field name="name">product.product.form.inherit</field>
<field name="model">product.product</field>
<field name="inherit_id" ref="product.product_normal_form_view"/>
<field name="arch" type="xml">
<xpath expr="//field[@name='default_code']" position="replace">
<field name="default_code"/>
</xpath>
</field>
</record>
</data>
</openerp>

Thank you for the help. Does anybody have any pointers about how to achieve the final goal: dynamic search, so that when i start to enter a reference, i get below a list of references that match what i have already entered. More exactly you can see this already implemented in Quotations - Create - Add an item - product column.

Avatar
Discard

Please read answer carefully. Answer is also useful when you implement your code as you have written. My answer is for old data not for the code or module installation error. Even module is successfully installed then also you can enter duplicate default_code if unique constraint is not applied. And I have stated that in some of the case module is installed but constraint is not applied. I hope you will also care for the old data. thanks.

Best Answer

are you sure you need to use inheritance by delegation as  _inherits = {'product.template': 'product_tmpl_id'}  ?

try with usual inheritance (a _inherit, NOT the _inherits):

_inherit = 'product.template'

refer to the documentation

Avatar
Discard
Best Answer

Try this friend:

class product_product(osv.osv):

_name = "product.product"

_description = "Product"

_inherit = ['product.template']

_sql_constraints = [

('default_code', 'unique(default_code)', 'Product Reference must be unique!'),

If it doesnt work

try this:

class product_product(osv.osv):

_name = "product.product"

_description = "Product"

_inherit = ['product.template']

_columns = {

'default_code' : fields.char('Internal Reference', select=True),

_sql_constraints = [

('default_code', 'unique(default_code)', 'Product Reference must be unique!'),

]

}

And dont forget to add in your XML:


<record id="view_product_form_inherit" model="ir.ui.view">

<field name="name">product.product.form.inherit</field>

<field name="model">product.product</field>

<field name="inherit_id" ref="product.product_normal_form_view"/>

<xpath expr="//field[@name='default_code']" position="replace">

<field name="default_code"/>

</xpath>

</field>

</record>

</data>

</openerp>

PS : Pay attention to the model and name of view in your XML.

Avatar
Discard
Best Answer

Hello,

According to the below code Odoo will add one unique constraint inside postgresql database.

class product_product(osv.osv):
_name = "product.product"
_description = "Product"
_inherit = 'product.product'

_sql_constraints = [
('default_code', 'unique(default_code)', 'Product Reference must be unique!'),
]
product_product()

But according to the already available records there may be some situations like :

1) When all records have default_code is already unique : In this case when Odoo is going to add unique constraint on the field default_code at that time inside database one constraint is added for the field default_code inside product_product table, and it is added successfully. After that you can also see the effect of that constraint and you can not make any duplicate value inside default_code field.

2) When all records have default_code is not unique / Already there are some duplicate value is there : In this case when Odoo is going to add unique constraint on the field default_code at that time inside database it is not possible to apply unique constraint on already duplicated data. So, at that time constraint will not perfectly done at database side and Odoo also give us WARNING :

"""Table 'product_product': unable to add 'unique(default_code)' constraint ! If you want to have it, you should update the records and execute manually:  ALTER TABLE "product_product" ADD CONSTRAINT "product_product_default_code" unique(default_code)"""  

So, in this case if we want to apply this constraint first of all we have to make changes inside database to do so all default_code value is unique and then after just upgrade the module again, Odoo will add that constraint on that field or you can execute manual query as give inside warning.


In your code might be the issue of the data not of code. I hope it is helpful to you for understanding that how _sql_constraint is work in Odoo.

Thanks.

Avatar
Discard
Related Posts Replies Views Activity
4
Mar 16
4056
2
Feb 16
2564
1
Feb 16
21268
0
Jan 16
3263
0
Dec 15
2397