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

Set defaults based on id

By
tudor
on 8/14/15, 5:42 AM 685 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.

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

Prakash
on 8/14/15, 5:52 AM
0
tudor
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

Prakash
on 8/14/15, 8:20 AM
0

Drees Far

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

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.


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