Skip to Content
Odoo Menu
  • Prijavi
  • Try it free
  • Aplikacije
    Finance
    • Knjigovodstvo
    • Obračun
    • Stroški
    • Spreadsheet (BI)
    • Dokumenti
    • Podpisovanje
    Prodaja
    • CRM
    • Prodaja
    • POS Shop
    • POS Restaurant
    • Naročnine
    • Najem
    Spletne strani
    • Website Builder
    • Spletna trgovina
    • Blog
    • Forum
    • Pogovor v živo
    • eUčenje
    Dobavna veriga
    • Zaloga
    • Proizvodnja
    • PLM
    • Nabava
    • Vzdrževanje
    • Kakovost
    Kadri
    • Kadri
    • Kadrovanje
    • Odsotnost
    • Ocenjevanja
    • Priporočila
    • Vozni park
    Marketing
    • Družbeno Trženje
    • Email Marketing
    • SMS Marketing
    • Dogodki
    • Avtomatizacija trženja
    • Ankete
    Storitve
    • Projekt
    • Časovnice
    • Storitve na terenu
    • Služba za pomoč
    • Načrtovanje
    • Termini
    Produktivnost
    • Razprave
    • Odobritve
    • IoT
    • Voip
    • Znanje
    • WhatsApp
    Third party apps Odoo Studio Odoo Cloud Platform
  • Industrije
    Trgovina na drobno
    • Book Store
    • Trgovina z oblačili
    • Trgovina s pohištvom
    • Grocery Store
    • Trgovina s strojno opremo računalnikov
    • Trgovina z igračami
    Food & Hospitality
    • Bar and Pub
    • Restavracija
    • Hitra hrana
    • Guest House
    • Beverage Distributor
    • Hotel
    Nepremičnine
    • Real Estate Agency
    • Arhitekturno podjetje
    • Gradbeništvo
    • Estate Management
    • Vrtnarjenje
    • Združenje lastnikov nepremičnin
    Svetovanje
    • Računovodsko podjetje
    • Odoo Partner
    • Marketinška agencija
    • Law firm
    • Pridobivanje talentov
    • Audit & Certification
    Proizvodnja
    • Tekstil
    • Metal
    • Pohištvo
    • Hrana
    • Brewery
    • Poslovna darila
    Health & Fitness
    • Športni klub
    • Trgovina z očali
    • Fitnes center
    • Wellness Practitioners
    • Lekarna
    • Frizerski salon
    Trades
    • Handyman
    • IT Hardware & Support
    • Sistemi sončne energije
    • Izdelovalec čevljev
    • Čistilne storitve
    • HVAC Services
    Ostali
    • Neprofitna organizacija
    • Agencija za okolje
    • Najem oglasnih panojev
    • Fotografija
    • Najem koles
    • Prodajalec programske opreme
    Browse all Industries
  • Skupnost
    Learn
    • Tutorials
    • Dokumentacija
    • Certifikati
    • Šolanje
    • Blog
    • Podcast
    Empower Education
    • Education Program
    • Scale Up! Business Game
    • Visit Odoo
    Get the Software
    • Prenesi
    • Compare Editions
    • Releases
    Collaborate
    • Github
    • Forum
    • Dogodki
    • Prevodi
    • Become a Partner
    • Services for Partners
    • Register your Accounting Firm
    Get Services
    • Find a Partner
    • Find an Accountant
    • Meet an advisor
    • Implementation Services
    • Sklici kupca
    • Podpora
    • Upgrades
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Get a demo
  • Določanje cen
  • Pomoč

Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:

  • CRM
  • e-Commerce
  • Knjigovodstvo
  • Zaloga
  • PoS
  • Projekt
  • MRP
All apps
You need to be registered to interact with the community.
All Posts People Badges
Ključne besede (View all)
odoo accounting v14 pos v15
About this forum
You need to be registered to interact with the community.
All Posts People Badges
Ključne besede (View all)
odoo accounting v14 pos v15
About this forum
Pomoč

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

Naroči se

Get notified when there's activity on this post

This question has been flagged
defaultspython2.7openerp7default_get
4 Odgovori
10962 Prikazi
Avatar
Anirudh Lou

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.

0
Avatar
Opusti
Avatar
Axel Mendoza
Best Answer

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

2
Avatar
Opusti
Anirudh Lou
Avtor

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
Avtor

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
Avtor

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

Axel Mendoza

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

Anirudh Lou
Avtor

Thanks! It helped me a lot.

Anirudh Lou
Avtor

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

Avatar
Pawan
Best Answer

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!    

0
Avatar
Opusti
Anirudh Lou
Avtor

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

Anirudh Lou
Avtor

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.

Pawan

check my updated answer....

Enjoying the discussion? Don't just read, join in!

Create an account today to enjoy exclusive features and engage with our awesome community!

Prijavi
Related Posts Odgovori Prikazi Aktivnost
How do I create a message box/ dialog box in openerp 7 with 'Ok' and 'Cancel' buttons in it
python2.7 openerp7
Avatar
Avatar
3
sep. 18
6288
Hide edit button according to status in openerp
python2.7 openerp7
Avatar
0
feb. 16
4375
How to get the sum of field in a column?
python2.7 openerp7
Avatar
0
avg. 15
6294
Default value of a field depending on a field of parent model
defaults default_get
Avatar
0
jul. 15
4664
How to show two fields from the same table whith on change
python2.7 openerp7
Avatar
Avatar
1
mar. 15
1231
Community
  • Tutorials
  • Dokumentacija
  • Forum
Open Source
  • Prenesi
  • Github
  • Runbot
  • Prevodi
Services
  • Odoo.sh Hosting
  • Podpora
  • Nadgradnja
  • Custom Developments
  • Izobraževanje
  • Find an Accountant
  • Find a Partner
  • Become a Partner
About us
  • Our company
  • Sredstva blagovne znamke
  • Kontakt
  • Zaposlitve
  • Dogodki
  • Podcast
  • Blog
  • Stranke
  • Pravno • Zasebnost
  • Varnost
الْعَرَبيّة Català 简体中文 繁體中文 (台灣) Čeština Dansk Nederlands English Suomi Français Deutsch हिंदी Bahasa Indonesia Italiano 日本語 한국어 (KR) Lietuvių kalba Język polski Português (BR) română русский язык Slovenský jazyk slovenščina Español (América Latina) Español ภาษาไทย Türkçe українська Tiếng Việt

Odoo is a suite of open source business apps that cover all your company needs: CRM, eCommerce, accounting, inventory, point of sale, project management, etc.

Odoo's unique value proposition is to be at the same time very easy to use and fully integrated.

Website made with

Odoo Experience on YouTube

1. Use the live chat to ask your questions.
2. The operator answers within a few minutes.

Live support on Youtube
Watch now