Thursday, 28 May 2015

How to set default value in Odoo ?

When User open a form view at that time default value display and than after save record it will display value based on our requirement.

Here is char type field example which will display a string before save record default and than it will show different string.

We will use function filed for that. This field will not take input from the user click that will only used for information purpose.

Here is code example:

def _default_get(self, cr, uid, context=None):
    print " This function called before new record create "
    res = 'bhavesh'
    return res     

def _set_value(self, cr, uid, ids, name, args, context=None):
    print " This function called at time of saving record and form view load "
    res = {}
    for i in self.browse(cr, uid, ids, context=context):
        res[i.id] = 'odedra'
    return res

_columns = {
    'value': fields.function(_set_value, type='char', string='Value'),
}

_defaults = {
    'value': _default_get,
}

I hope you like this article. Share your views to improve content. Happy Learning !!! 

3 comments:

  1. Hi guy, how can I set another field value as a default?
    I tried browser it but:
    product_obj = self.pool.get('product.product').browse(cr, uid, ids[0], context=context)

    as I don't have ids parameter, Idk how to look for it x)

    Regards

    ReplyDelete
    Replies
    1. Default method is used for getting value from the other object. If you want to get value from the browse record than you need to make on_change method. Based on selected product, you may play with it.

      Delete
  2. is there any change to this approach in odoo 9? Struggling a bit to get this thing right...

    ReplyDelete

ImportError: No module named googleapiclient

Recently, I have faced ImportError: No module named googleapiclient and I would like to share knowledge to fix that problem. Open termina...