Odoo Help

0

When We can use default_get()

By
Odoo
on 2/12/15, 2:24 AM 1,186 views

Hi all,

At  which scenario we can use default_get() methods  in  a openerp module.Please explain with an example.

Thanks in advance..

0

default_get is mainly used when we have to compute based on other field (or context) and to set values for multiple fields that are interdependent

default_get(self....):
      res=super()
      field1 = obtain field1 value through context and change behaviour of values loaded
      field2 depends on field1 value
      field3 depends on field2 value
      res.update({'field1': field1,'field2':field2,'field3': field3})
      return res

1

def default_get(self, cr, uid, fields, context=None):
        res = super(stock_return_picking, self).default_get(cr, uid, fields, context=context)
        record_id = context and context.get('active_id', False) or False
        pick_obj = self.pool.get('stock.picking')
        pick = pick_obj.browse(cr, uid, record_id, context=context)
        if pick:
            if 'invoice_state' in fields:
                if pick.invoice_state=='invoiced':
                    res.update({'invoice_state': '2binvoiced'})
                else:
                    res.update({'invoice_state': 'none'})
        return res

 

1
Zbik
On 2/12/15, 5:14 AM

default_get(cr, uid, fields_list, context=None)

Returns default values for the fields in fields_list.

Parameters:

  • fields_list (list) -- list of fields to get the default values for (example ['field1', 'field2',])

  • context -- optional context dictionary - it may contains keys for specifying certain options like context_lang (language) or context_tz (timezone) to alter the results of the call. It may contain keys in the form default_XXX (where XXX is a field name), to set or override a default value for a field. A special bin_size boolean flag may also be passed in the context to request the value of all fields.binary columns to be returned as the size of the binary instead of its contents. This can also be selectively overriden by passing a field-specific flag in the form bin_size_XXX: True/False where XXX is the name of the field. Note: The bin_size_XXX form is new in OpenERP v6.0.

Returns:

dictionary of the default values (set on the object model class, through user preferences, or in the context)

Example, from stock_return_picking.py, default value of invoice_state in return picking depends on the state of previous picking (invoiced or not):

    def default_get(self, cr, uid, fields, context=None):
        res = super(stock_return_picking, self).default_get(cr, uid, fields, context=context)
        record_id = context and context.get('active_id', False) or False
        pick_obj = self.pool.get('stock.picking')
        pick = pick_obj.browse(cr, uid, record_id, context=context)
        if pick:
            if 'invoice_state' in fields:
                if pick.invoice_state=='invoiced':
                    res.update({'invoice_state': '2binvoiced'})
                else:
                    res.update({'invoice_state': 'none'})
        return res

 

 

Thanks for ur answer.. zbik

Odoo
on 2/12/15, 6:22 AM

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.

Register

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

2 follower(s)

Stats

Asked: 2/12/15, 2:24 AM
Seen: 1186 times
Last updated: 3/16/15, 8:10 AM