Skip to Content
Odoo Menú
  • Registra entrada
  • Prova-ho gratis
  • Aplicacions
    Finances
    • Comptabilitat
    • Facturació
    • Despeses
    • Full de càlcul (IA)
    • Documents
    • Signatura
    Vendes
    • CRM
    • Vendes
    • Punt de venda per a botigues
    • Punt de venda per a restaurants
    • Subscripcions
    • Lloguer
    Imatges de llocs web
    • Creació de llocs web
    • Comerç electrònic
    • Blog
    • Fòrum
    • Xat en directe
    • Aprenentatge en línia
    Cadena de subministrament
    • Inventari
    • Fabricació
    • PLM
    • Compres
    • Manteniment
    • Qualitat
    Recursos humans
    • Empleats
    • Reclutament
    • Absències
    • Avaluacions
    • Recomanacions
    • Flota
    Màrqueting
    • Màrqueting Social
    • Màrqueting per correu electrònic
    • Màrqueting per SMS
    • Esdeveniments
    • Automatització del màrqueting
    • Enquestes
    Serveis
    • Projectes
    • Fulls d'hores
    • Servei de camp
    • Suport
    • Planificació
    • Cites
    Productivitat
    • Converses
    • Validacions
    • IoT
    • VoIP
    • Coneixements
    • WhatsApp
    Aplicacions de tercers Odoo Studio Plataforma d'Odoo al núvol
  • Sectors
    Comerç al detall
    • Llibreria
    • Botiga de roba
    • Botiga de mobles
    • Botiga d'ultramarins
    • Ferreteria
    • Botiga de joguines
    Food & Hospitality
    • Bar i pub
    • Restaurant
    • Menjar ràpid
    • Guest House
    • Distribuïdor de begudes
    • Hotel
    Immobiliari
    • Agència immobiliària
    • Estudi d'arquitectura
    • Construcció
    • Gestió immobiliària
    • Jardineria
    • Associació de propietaris de béns immobles
    Consultoria
    • Empresa comptable
    • Partner d'Odoo
    • Agència de màrqueting
    • Bufet d'advocats
    • Captació de talent
    • Auditoria i certificació
    Fabricació
    • Textile
    • Metal
    • Mobles
    • Menjar
    • Brewery
    • Regals corporatius
    Salut i fitness
    • Club d'esport
    • Òptica
    • Centre de fitness
    • Especialistes en benestar
    • Farmàcia
    • Perruqueria
    Trades
    • Servei de manteniment
    • Hardware i suport informàtic
    • Sistemes d'energia solar
    • Shoe Maker
    • Serveis de neteja
    • Instal·lacions HVAC
    Altres
    • Nonprofit Organization
    • Agència del medi ambient
    • Lloguer de panells publicitaris
    • Fotografia
    • Lloguer de bicicletes
    • Distribuïdors de programari
    Browse all Industries
  • Comunitat
    Aprèn
    • Tutorials
    • Documentació
    • Certificacions
    • Formació
    • Blog
    • Pòdcast
    Potenciar l'educació
    • Programa educatiu
    • Scale-Up! El joc empresarial
    • Visita Odoo
    Obtindre el programari
    • Descarregar
    • Comparar edicions
    • Novetats de les versions
    Col·laborar
    • GitHub
    • Fòrum
    • Esdeveniments
    • Traduccions
    • Converteix-te en partner
    • Services for Partners
    • Registra la teva empresa comptable
    Obtindre els serveis
    • Troba un partner
    • Troba un comptable
    • Contacta amb un expert
    • Serveis d'implementació
    • Referències del client
    • Suport
    • Actualitzacions
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Programar una demo
  • Preus
  • Ajuda

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

  • CRM
  • e-Commerce
  • Comptabilitat
  • Inventari
  • PoS
  • Projectes
  • MRP
All apps
You need to be registered to interact with the community.
All Posts People Badges
Etiquetes (View all)
odoo accounting v14 pos v15
About this forum
You need to be registered to interact with the community.
All Posts People Badges
Etiquetes (View all)
odoo accounting v14 pos v15
About this forum
Ajuda

Use Onchange on one record to change fields on other records.

Subscriure's

Get notified when there's activity on this post

This question has been flagged
saleonchangev14
2 Respostes
45449 Vistes
Avatar
Dries Cox

Hello Community,
I am writing some code that changes the value of a field in all the lines of a sales order.  So if a user changed the category_id in one sale order line, other lines should be change this categ_id as well.

But for some reason this is not working. The values on other lines are not updated and stay the same like nothing happened. 

class SaleOrderLine(models.Model):
_inherit="sale.order.line"

@api.onchange('categ_id')
def _onchange_categ_id(self):
    
    for changed_line in self:
            #find all lines of the parent sale_order
             all_lines = changed_line.order_id.order_line
            vals = {'categ_id': changed_line.categ_id}
            for line in all_lines:
                line.update(vals)
    return {}

-> update to give more clarification on what the reason behind this code is.

The reason for this that, in the bigger picture, is that I can create groups of sale order lines, separated by line_sections. Each group of lines has the same categ_id, so if the categ_id of a section changes, all the lines in this section should change to the categ_id as well.

All the help is welcome.

Greetings,

1
Avatar
Descartar
Cayprol

In that case I think it's not possible,

In the ORM API documentation,

It is not possible for a one2many or many2many field to modify itself via onchange. This is a webclient limitation - see #2693.

https://www.odoo.com/documentation/13.0/reference/orm.html

https://github.com/odoo/odoo/issues/2693

Dries Cox
Autor

I think you're right. That is a pity, I will have to look for an other solution.

can you change your remark to an answer so we can set that as the right answer? Thanks again for the help.

Avatar
Dries Cox
Autor Best Answer

Hello, I found a solution for this problem.

I changed the onchange function that happens in the line of a One2many so it sets a flag that it is changed.

Then I added a onchange function on the "order_line"-field in the order model. This onchange method is triggered as well after a line changed and it is triggered the last in line after a change in the records.

In the onchange-orderline function I check the states of the flags, reset them and preform the necessary action using a function call on the sale object. (don't know if it is necessary to do it with a function call, but I saw that in sale.order.line onchange fiscal postition)

That does the job. The lines are changed on-screen!

class SaleOrderLine(models.Model):
    _inherit="sale.order.line"

    categ_changed = fields.Boolean("technical field, do not use or show to user", default = False)

    @api.onchange('categ_id')
    def _onchange_categ_id(self)
        for line in self:
            line.categ_changed = True
        return True

    def change_categs(self, changed_lines_lst)
        for line in self
            if self.id in changed_lines_lst:
                self.categ_changed = False
                #do necessary update actions within this line
        return

class SaleOrder(models.Model):
    _inherit="sale.order"

    @api.onchange('order_line')
    def _onchange_order_line(self)
        #we need to get this from te context because object might not be the same as screen values.
        ctx_lines = self.env.context.get('order_line')
        changed_lines_lst = []
        if ctx_lines:
            for ctx_line in ctx_lines:
                if ctx_line[2]:
                    if ctx_line[2].get("categ_changed")
                        changed_lines_lst.append(ctx_line[1])

           for order in self:
                self.order_line.change_categs(changed_lines_lst)

adding context to view:
<xpath expr="//field[@name='order_line']" position="attributes">
<attribute
name="context">{"order_line": order_line}</attribute>
</xpath>


Good luck :-)
3
Avatar
Descartar
Avatar
Cayprol
Best Answer

Update:

In that case I think it's not possible,

In the ORM API documentation,

It is not possible for a one2many or many2many field to modify itself via onchange. This is a webclient limitation - see #2693.

https://www.odoo.com/documentation/13.0/reference/orm.html

https://github.com/odoo/odoo/issues/2693


If I understand correctly,

You are trying to change a field name 'categ_id' in model 'sale.order', and you modify this field from a view in model 'sale.order', likely from view_id 'sale.view_order_form'

The expected behavior is that all 'order_line' records will have a corresponding field to be changed to the same.

If so, you don't need to use onchange, I use a Char field named 'categ_idd' as an example

class SaleOrder(models.Model): 
    _inherit = 'sale.order'
    categ_idd = fields.Char('Categ IDD')

class SaleOrderLine(models.Model):
    _inherit = 'sale.order.line'
    categ_idd = fields.Char('Categ IDD', related='order_id.categ_idd')
0
Avatar
Descartar
Dries Cox
Autor

Hello, this is not really what I am looking for. It should be that categ_id is a field of the sale.order.line and if the field categ_id is changed on one line it should change on other sale.order.lines as well.

The reason for this that, in the bigger picture, i can create groups of sale order lines, separated by line_sections. Each group of lines has the same categ_id, so if the categ_id of a section changes, all the lines in this section should change as well to the same categ_id. -> I will add this explanation to my question as well this might clarify my question more. Thank you for your answer.

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

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

Registrar-se
Related Posts Respostes Vistes Activitat
Onchange not triggered when record is altered from python code Solved
onchange v14
Avatar
Avatar
1
de des. 21
7087
Is there a way to create dependencies between sale order lines?
sale v14
Avatar
Avatar
1
d’abr. 21
2622
Pause user interface during the calculation
onchange v14
Avatar
Avatar
2
de des. 20
4880
Set UOM and precision on product without inventory module.
invoice sale v14
Avatar
1
de juny 21
2651
How to set a Dynamic domain in odooV14 on a One2many list?
sale domain_filter dynamic v14
Avatar
Avatar
1
de maig 24
6995
Community
  • Tutorials
  • Documentació
  • Fòrum
Codi obert
  • Descarregar
  • GitHub
  • Runbot
  • Traduccions
Serveis
  • Allotjament a Odoo.sh
  • Suport
  • Actualització
  • Desenvolupaments personalitzats
  • Educació
  • Troba un comptable
  • Troba un partner
  • Converteix-te en partner
Sobre nosaltres
  • La nostra empresa
  • Actius de marca
  • Contacta amb nosaltres
  • Llocs de treball
  • Esdeveniments
  • Pòdcast
  • Blog
  • Clients
  • Informació legal • Privacitat
  • Seguretat
الْعَرَبيّة 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 és un conjunt d'aplicacions empresarials de codi obert que cobreix totes les necessitats de la teva empresa: CRM, comerç electrònic, comptabilitat, inventari, punt de venda, gestió de projectes, etc.

La proposta única de valor d'Odoo és ser molt fàcil d'utilitzar i estar totalment integrat, ambdues alhora.

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