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.

0

How to edit product field

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

Temur

--Temur--
2894
| 6 5 7
Tbilisi, Georgia
--Temur--
Programmer
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

1

Drees Far

--Drees Far--
1130
| 5 2 6
Tunis, Tunisia
--Drees Far--
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.

0

Emipro Technologies Pvt. Ltd.

--Emipro Technologies Pvt. Ltd.--
Tiny ERP Pvt Ltd
http://www.odoo.com
5975
| 6 7 8
Rajkot, India
--Emipro Technologies Pvt. Ltd.--

Being an emerging leader in IT market since 2011, Emipro Technologies Pvt. Ltd. has been providing a wide range of business solutions in Odoo & Magento. We are pleased to have a large pool of contented customers with our meticulous work in the domain of ERP & e-Commerce. A ray of relief & satisfaction to  our customers heart by our successful deployment in their organization, purely represents our success in Odoo platform. Hence, we take pride for being an Odoo partner with a vision of expanding our strategic alliances with our customers to offer them high value-added, trustworthy & cost effective solutions.

 

Since establishment, our business has span across 11 countries of the world. Our customers are companies of all sizes ranging from start-ups to large enterprises, who realize that they need a professional business solutions to generate revenue streams, establish proper communication channels or streamline business operations. Standing with strong determination of customer satisfaction, observing each minute detail of their business processes, providing proper guidance and moving forth to develop product accordingly adds our value and reliability among our customers. 

 

We are blessed with efficient, passionate & eclectic young developers who have come across almost all kinds of business profiles, working with full dedication, applying creativity & new features in existing modules and completing customer's projects successfully on predefined target. Our in-depth knowledge while giving business solutions in Odoo allows us to offer following services to our customers :

 

  • Consulting
  • Installation, Configuration & Customization
  • Training & Support
  • Maintenance
  • Upgradation
  • Documentation
  • Crafting community modules
  • Drafting videos on demand
  • Smart Apps development

We will be :) to welcome you @ www.emiprotechnologies.com to solve any kind of your business needs around Odoo. However we are just an inch away from you by email info@emiprotechnologies.com or a tweet to @EmiproTech

Emipro Technologies Pvt. Ltd.
On 8/8/15, 5:42 AM

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.

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

1 follower(s)

Stats

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