Odoo Help

0

How to edit product field

By
tudor
on 8/7/15, 8:49 AM 1,623 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

0
tudor
On 8/7/15, 9:43 AM

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.

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.

Emipro Technologies Pvt. Ltd.
on 8/10/15, 5:32 AM
1
Drees Far
On 8/7/15, 9:58 AM

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.

1
Temur
On 8/7/15, 9:28 AM

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

0

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.

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.

Register

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)

Stats

Asked: 8/7/15, 8:49 AM
Seen: 1623 times
Last updated: 8/10/15, 4:46 AM