python 2.7 - How to filter fields in one2many base on it's many2one? -


we know class sale_order has sale_order_line. wondering how can filter/set default value fields.

here illustration.

in class res_partner, add field of type selection:

'part_type' : fields.selection([('child','child'),('adult','adult'),('senior','senior')],'partner type') 

in sale_order_line, add this:

'rule_applied' : fields.selection([('low','low'),('lower','lower'),('lowest','lowest')],'rule')  

now, if user selects partner in sale_order , if tries add item on sale_order_line, want set default value of rule_applied according criteria:

if partner_type of selected partner child default value of rule_applied lowest, if senior lower , if adult low.

i tried method not work:

def default_get(self, cr, uid, fields, context=none):     res = super(sale_order_line, self).default_get(cr, uid, fields, context=context)     _logger.info("\n\n\t\t\tsetting rule >>> fields %s"%(str(context)))     _logger.info("\n\n\t\t\tsetting rule >>> res %s"%(str(res)))     if 'order_partner_id' in fields:         partner_id = res['order_partner_id']         order_id = res['order_id']         if order_partner_id:             part_list = self.pool.get('res.partner').search(cr,uid,[('customer','=',true)])             partner = self.pool.get('res.partner').browse(cr,uid,part_list,context=context)             if partner.part_type == 'child':                 res['rule_applied'] = 'lowest'             elif partner.part_type == 'adult':                 res['rule_applied'] = 'low'             elif partner.part_type == 'senior':                 res['rule_applied'] = 'lowe'                             else:                 res['rule_applied'] = false         if order_id:             order = self.pool.get('sale.order').browse(cr,uid,order_id,context=context)             partner = self.pool.get('res.partner').browse(cr,uid,order.partner_id.id,context=context)             if partner.part_type == 'child':                 res['rule_applied'] = 'lowest'             elif partner.part_type == 'adult':                 res['rule_applied'] = 'low'             elif partner.part_type == 'senior':                 res['rule_applied'] = 'lowe'                             else:                 res['rule_applied'] = false      return res  

any appreciated

default_get returns dictionary of default values fields(set on object class, user preferences, or via context).

so need update dictionary our desire value using .update({'field_name': value}) method.

try code:

def default_get(self, cr, uid, fields, context=none):     res = super(sale_order_line, self).default_get(cr, uid, fields, context=context)     _logger.info("\n\n\t\t\tsetting rule >>> fields %s"%(str(context)))     _logger.info("\n\n\t\t\tsetting rule >>> res %s"%(str(res)))     if 'order_partner_id' in fields:         partner_id = res['order_partner_id']         order_id = res['order_id']         if order_partner_id:             part_list = self.pool.get('res.partner').search(cr,uid,[('customer','=',true)])             partner = self.pool.get('res.partner').browse(cr,uid,part_list,context=context)             if partner.part_type == 'child':                 res.update({'rule_applied': 'lowest'})             elif partner.part_type == 'adult':                 res.update({'rule_applied': 'low'})             elif partner.part_type == 'senior':                 res.update({'rule_applied': 'lowe'})                           else:                 res.update({'rule_applied': false})         if order_id:             order = self.pool.get('sale.order').browse(cr,uid,order_id,context=context)             partner = self.pool.get('res.partner').browse(cr,uid,order.partner_id.id,context=context)             if partner.part_type == 'child':                 res.update({'rule_applied': 'lowest'})             elif partner.part_type == 'adult':                 res.update({'rule_applied': 'low'})             elif partner.part_type == 'senior':                 res.update({'rule_applied': 'lowe'})                        else:                 res.update({'rule_applied': false})      return res  

Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -