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

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

By
Anirudh Lou
on 8/25/15, 3:39 AM 1,506 views

We know that in class sale_order has sale_order_line. I am wondering now how can i filter/set default value for some fields. Here is my illustration.

In class res_partner i add some field of type selection:

'part_type' : fields.selection([('child','Child'),('adult','Adult'),('senior','Senior')],'Partner Type')

In sale_order_line i also add this:

'rule_applied' : fields.selection([('low','Low'),('lower','Lower'),('lowest','Lowest')],'Rule') 

Now, if a user select a partner in sale_order and if he try to add an item on its sale_order_line i want to set the default value of rule_applied according to this criteria: If the partner_type of partner selected is child then the default value of rule_applied is Lowest, if senior then Lower and if adult then Low. I tried this method but it does 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 up rule >>> fields %s"%(str(context)))
_logger.info("\n\n\t\t\tSetting up rule >>> res %s"%(str(res)))
if 'partner_id' in fields or 'order_id' in fields:
partner_id = res['partner_id']
order_id = res['order_id']
if 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

But it does not work. upon running, my does not work.

Help is much needed.

2

Axel Mendoza

--Axel Mendoza--
10238
| 7 7 8
Camaguey, Cuba
--Axel Mendoza--

DevOps - Full stack - Software Architect - Developer - Technology Integrator

I could help you to develop anything and solve complex problems based on technologies, integrations and tricky stuffs mostly in Python with OpenERP/Odoo, Zato, Django and many others frameworks programming languages and technologies.

I offers consulting services to anyone with an unanswered questions or needs for customizations. Think about it, maybe it's better to have an expert to solve your issues and projects than having a full time employee trying to understand what to do an how

Reach me at aekroft@gmail.com

Axel Mendoza
On 8/25/15, 7:55 AM

If I need to code this I will do the following steps:

Add a field to sale.order to use it only for determine the default value to be passed to the order_lines fields

'rule_tpl' : fields.dummy(type="selection",selection=[('low','Low'),('lower','Lower'),('lowest','Lowest')],string='Rule') 

Create an onchange method on the partner field to return the value of the new field rule_tpl using your conditions, that method already exist for sale.order you will be overriding in order to add more values, for that just call super() of your class (for example for the class sale_order will be super(sale_order, self) ). Something like this:

class sale_order(osv.osv):
_name = 'sale.order'
_inherit = 'sale.order'
...
...
def onchange_partner_id(self, cr, uid, ids, partner_id, context=None):
        res = super(sale_order, self).onchange_partner_id(cr, uid, ids, partner_id, context=context)
        partner = self.pool.get('res.partner').browse(cr,uid,partner_id,context=context)
if partner.part_type == 'child':
  res['value']['rule_tpl'] = 'lowest'
elif partner.part_type == 'adult':
res['value']['rule_tpl'] = 'low'
elif partner.part_type == 'senior':
res['value']['rule_tpl'] = 'lower'
else:
res['value']['rule_applied'] = False
return res
...
... 

sale_order()

then in sale.order view form you need to declare the new field rule_tpl with invisible="1" and use it in the context passed to the field order_lines, like this example:


        <record model="ir.ui.view" id=field_tpl_sale_order">
<field name="name">field.tpl.sale.order.view.form</field>
<field name="model">sale.order</field>
<field name="inherit_id" ref="sale.view_order_form" />
<field name="arch" type="xml">
<field name="origin" position="after">
<field name="rule_tpl" invisible="1"/>
</field>
<field name="order_lines" position="attributes">
<attribute name="context">{'default_rule_applied': rule_tpl}</attribute>
</field>
</field>
</record>

With this setup I think that you will be ok, the default_ values passed in the context of relation fields cause that records created in that field assume those values as defaults

Okay thanks a lot. I'll try it tomorrow at computer lab. By the way in that case do i still need to define default_get in sale_order_line or no need?

Anirudh Lou
on 8/25/15, 9:54 AM

Why does overriding onchange_partner_id produce a client error, it says: "TypeError: val is undefined" i also tried to copy the original method and add what you suggested but same error output.

Anirudh Lou
on 8/25/15, 10:03 PM

I guess it's because my field is of type dummy. So, with all due respect, i change it to a regular field selection.

Anirudh Lou
on 8/25/15, 10:16 PM

The error you mentioned could be more verbosed?? With what you said is impossible to know what is causing it. I don't think that dummy is the source of the error. Please post the error log

Axel Mendoza
on 8/25/15, 11:27 PM

Thanks! It helped me a lot.

Anirudh Lou
on 8/26/15, 1:10 AM

with fields.dummy this is what i get: OpenERP Client Error TypeError: val is undefined http://localhost:8069/web/static/src/js/pyeval.js:685

Anirudh Lou
on 8/26/15, 1:17 AM
0

Pawan

--Pawan--
1267
| 4 3 5
Hyderabad, India
--Pawan--


Pawan
On 8/25/15, 4:08 AM

Anirudh,
    

So, If it is not there in res, then ... pass the partner_id in context of sale_order_line(one2many) field and then you can easily get it in default get of sale order line context, which you already implemented ..

There in default_get use that partner_id for your further operation...    

hope it helps!    

Yeah, thanks for pointing it out it's order_partner_id, but it's not present in res.

Anirudh Lou
on 8/25/15, 4:44 AM

From sale_order_line how can i get the value of partner_id in sale_order? I think i cannot rely on order_partner_id because it is of type related and by default it has no value. Just correct me if i am wrong.

Anirudh Lou
on 8/25/15, 5:48 AM

check my updated answer....

Pawan
on 8/25/15, 8:11 AM

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

2 follower(s)

Stats

Asked: 8/25/15, 3:39 AM
Seen: 1506 times
Last updated: 8/26/15, 1:18 AM