Skip to Content
Odoo Meniu
  • Autentificare
  • Try it free
  • Aplicații
    Finanțe
    • Contabilitate
    • Facturare
    • Cheltuieli
    • Spreadsheet (BI)
    • Documente
    • Semn
    Vânzări
    • CRM
    • Vânzări
    • POS Shop
    • POS Restaurant
    • Abonamente
    • Închiriere
    Site-uri web
    • Constructor de site-uri
    • eCommerce
    • Blog
    • Forum
    • Live Chat
    • eLearning
    Lanț Aprovizionare
    • Inventar
    • Producție
    • PLM
    • Achiziție
    • Maintenance
    • Calitate
    Resurse Umane
    • Angajați
    • Recrutare
    • Time Off
    • Evaluări
    • Referințe
    • Flotă
    Marketing
    • Social Marketing
    • Marketing prin email
    • SMS Marketing
    • Evenimente
    • Automatizare marketing
    • Sondaje
    Servicii
    • Proiect
    • Foi de pontaj
    • Servicii de teren
    • Centru de asistență
    • Planificare
    • Programări
    Productivitate
    • Discuss
    • Aprobări
    • IoT
    • VoIP
    • Knowledge
    • WhatsApp
    Aplicații Terțe Odoo Studio Platforma Odoo Cloud
  • Industrii
    Retail
    • Book Store
    • Magazin de îmbrăcăminte
    • Magazin de Mobilă
    • Magazin alimentar
    • Magazin de materiale de construcții
    • Magazin de jucării
    Food & Hospitality
    • Bar and Pub
    • Restaurant
    • Fast Food
    • Guest House
    • Distribuitor de băuturi
    • Hotel
    Proprietate imobiliara
    • Real Estate Agency
    • Firmă de Arhitectură
    • Construcție
    • Estate Managament
    • Grădinărit
    • Asociația Proprietarilor de Proprietăți
    Consultanta
    • Firma de Contabilitate
    • Partener Odoo
    • Agenție de marketing
    • Law firm
    • Atragere de talente
    • Audit & Certification
    Producție
    • Textil
    • Metal
    • Mobilier
    • Mâncare
    • Brewery
    • Cadouri corporate
    Health & Fitness
    • Club Sportiv
    • Magazin de ochelari
    • Centru de Fitness
    • Wellness Practitioners
    • Farmacie
    • Salon de coafură
    Trades
    • Handyman
    • IT Hardware and Support
    • Asigurare socială de stat
    • Cizmar
    • Servicii de curățenie
    • HVAC Services
    Altele
    • Organizație nonprofit
    • Agenție de Mediu
    • Închiriere panouri publicitare
    • Fotografie
    • Închiriere biciclete
    • Asigurare socială
    Browse all Industries
  • Comunitate
    Învăță
    • Tutorials
    • Documentație
    • Certificări
    • Instruire
    • Blog
    • Podcast
    Empower Education
    • Program Educațional
    • Scale Up! Business Game
    • Visit Odoo
    Obține Software-ul
    • Descărcare
    • Compară Edițiile
    • Lansări
    Colaborați
    • Github
    • Forum
    • Evenimente
    • Translations
    • Devino Partener
    • Services for Partners
    • Înregistrează-ți Firma de Contabilitate
    Obține Servicii
    • Găsește un Partener
    • Găsiți un contabil
    • Meet an advisor
    • Servicii de Implementare
    • Referințe ale clienților
    • Suport
    • Actualizări
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Obține un demo
  • Prețuri
  • Ajutor

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

  • CRM
  • e-Commerce
  • Contabilitate
  • Inventar
  • PoS
  • Proiect
  • MRP
All apps
Trebuie să fiți înregistrat pentru a interacționa cu comunitatea.
All Posts Oameni Insigne
Etichete (View all)
odoo accounting v14 pos v15
Despre acest forum
Trebuie să fiți înregistrat pentru a interacționa cu comunitatea.
All Posts Oameni Insigne
Etichete (View all)
odoo accounting v14 pos v15
Despre acest forum
Suport

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

Abonare

Primiți o notificare când există activitate la acestă postare

Această întrebare a fost marcată
defaultspython2.7openerp7default_get
4 Răspunsuri
10970 Vizualizări
Imagine profil
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
Imagine profil
Abandonează
Imagine profil
Axel Mendoza
Cel mai bun răspuns

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
Imagine profil
Abandonează
Anirudh Lou
Autor

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
Autor

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
Autor

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
Autor

Thanks! It helped me a lot.

Anirudh Lou
Autor

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

Imagine profil
Pawan
Cel mai bun răspuns

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
Imagine profil
Abandonează
Anirudh Lou
Autor

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

Anirudh Lou
Autor

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!

Înscrie-te
Related Posts Răspunsuri Vizualizări Activitate
How do I create a message box/ dialog box in openerp 7 with 'Ok' and 'Cancel' buttons in it
python2.7 openerp7
Imagine profil
Imagine profil
3
sept. 18
6300
Hide edit button according to status in openerp
python2.7 openerp7
Imagine profil
0
feb. 16
4383
How to get the sum of field in a column?
python2.7 openerp7
Imagine profil
0
aug. 15
6305
Default value of a field depending on a field of parent model
defaults default_get
Imagine profil
0
iul. 15
4670
How to show two fields from the same table whith on change
python2.7 openerp7
Imagine profil
Imagine profil
1
mar. 15
1243
Comunitate
  • Tutorials
  • Documentație
  • Forum
Open Source
  • Descărcare
  • Github
  • Runbot
  • Translations
Servicii
  • Hosting Odoo.sh
  • Suport
  • Actualizare
  • Custom Developments
  • Educație
  • Găsiți un contabil
  • Găsește un Partener
  • Devino Partener
Despre Noi
  • Compania noastră
  • Active de marcă
  • Contactați-ne
  • Locuri de muncă
  • Evenimente
  • Podcast
  • Blog
  • Clienți
  • Aspecte juridice • Confidențialitate
  • Securitate
الْعَرَبيّة 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 este o suită de aplicații de afaceri open source care acoperă toate nevoile companiei dvs.: CRM, comerț electronic, contabilitate, inventar, punct de vânzare, management de proiect etc.

Propunerea de valoare unică a Odoo este să fie în același timp foarte ușor de utilizat și complet integrat.

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