Odoo Help


Set defaults based on id

on 8/14/15, 5:42 AM 1,434 views


I am trying to set some default values, but i can't figure it out. I have added a many2one field to the Product model, which i would like to have the default value of default_code (internal reference) which is a char field. In the _defaults i call the _get_default_code function, where i assume i have to first find the current product and get its default code (i don't know how to do this), then create a new product.code with this value, then return this new product.code. This is what i have so far:

class product_code(osv.osv):
_name = 'product.code'
_columns = {
'name': fields.char('Name', required=True, translate=True),

class product_legacy(osv.osv):

_inherit = 'product.template'
_columns = {
'default_code': fields.many2one('product.code', 'Internal Reference'),

def _get_default_code(self, cr, uid, context=None):
#get current product's default code
#res = self.pool.get('product.template').search(???)

code_obj = self.pool.get('product.code')
code_obj.create(cr, uid, {
'name': res['name']
}, context=context)

return code_obj

_defaults = {

Even if i put a test value instead of "res['name']" it doesn't do anything. When i create a new product or edit an existing product, the many2one field is still empty.

Thank you for the help.


on 8/14/15, 5:52 AM
On 8/14/15, 7:18 AM

Thank you for the answers. I think i didn't do a very good job explaining my problem. I try again. So, i have the Sales module installed with many products already defined. All the products already have set an 'Internal reference' (default_code). This default_code is of type char.

I have created a module that changes this default_code from char to many2one and references my product.code. This product.code is not defined in the view. I only use it to have this many2one.

The problem is that, as I said, I already have many products with 'internal reference' defined. I would like that when I install my new module, all the 'internal references' already defined to become many2one, with their previous value already set.

So I thought that I can achieve this by setting the _defaults to search for the product with the same id, get the 'internal reference', create a new product.code with it, and set it to the new many2one 'internal reference'.

I don't understand how to search for the product with the same id. Also, even if i return a dummy code_obj from my _get_default_code function, it does not work. The new many2one 'internal reference' is still empty.

Maybe I can't do it like this? Maybe I need to do an update on the database or something.

@Prakash i have already read your post, but i can't figure out how to search for the product with the current id. I assume i have to do something like

self.pool.get('product.product').search(cr, uid, [('id','=',???)], context=context) 

But i don't know what value to search for. Also, as i said, event if I don't use the search and put a dummy code_obj, i get nothing.

@Drees Far I want to have the values set when I install the module, not when I change something. As i said, i think i didn't explain it very well the first time, but maybe now it's more clear.

Thank you

Add on_change for product_id field and using browse method get many2one value. if product_id: product_code = product_obj.browse(cr,uid,product_id).product_code.id

on 8/14/15, 8:20 AM

Vale Trood

--Vale Trood--

| 5 2 7
Tunis, France
--Vale Trood--

Vale Trood
On 8/14/15, 5:51 AM


Hey friend you can add an on_change function when you click on the code_obj the many2one field will get its containing like this:


def on_change_state_id(self, cr, uid, ids,code_obj, many_2_one_field, context=None):

if code_obj :
       print code_obj
             res = {'value':{'many_2_one_field':code_obj,
         return res


My_error_Msg = 'Error : Empty Field'

raise osv.except_osv(_("Error!"), _(My_error_Msg))


<field name="code_obj" on_change="on_change_code_id(code_obj, many_2_one_field)"/>


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: 8/14/15, 5:42 AM
Seen: 1434 times
Last updated: 8/14/15, 7:18 AM