Skip to Content
Menu
This question has been flagged
3 Replies
6896 Views

Hello,

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 = {
'default_code':_get_default_code,
}

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.

Avatar
Discard

https://www.odoo.com/es_ES/forum/help-1/question/set-default-value-in-object-of-many2one-field-39425

Author Best Answer

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

Avatar
Discard

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

Best Answer

 

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:

Python:

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

else:

My_error_Msg = 'Error : Empty Field'

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

XML:

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

Regards.


Avatar
Discard
Related Posts Replies Views Activity
0
Jun 16
3030
2
Feb 16
6336
1
Aug 23
12549
1
Aug 23
11056
1
Jul 23
7228