Help

1

When We can use default_get()

Avatar
ABU K

Hi all,

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

Thanks in advance..

Avatar
Discard
3 Answers
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

Avatar
Discard
4
Avatar
Zbik
Best Answer

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

 

 

1 Comment
Avatar
Discard
Avatar
ABU K
-

Thanks for ur answer.. zbik

1
Best Answer

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

 

Avatar
Discard