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

Hierarchy relationship

Subscriure's

Get notified when there's activity on this post

This question has been flagged
v14
5216 Vistes
Avatar
Răzvan Anastasescu

I have extended Sale Order Line with parent / child hierarchy relationship because some lines (children) can be connected to others (parent) in the same Sale Order

class SaleOrderLine(models.Model):
_inherit = 'sale.order.line'
  _parent_name = 'my_parent_id'

my_parent_id = fields.Many2one('sale.order.line', 'Operation for', index=True, domain='[("my_parent_id", "=", False)]', ondelete='cascade')

my_children_ids = fields.One2many('sale.order.line', 'my_parent_id', 'Operations')

I am presenting children records differently in the view (different row color, disabled re-sequennce, and so on) based on the `my_parent_id` field (and I am also dealing with re-sequencing of the children together with the parent and all the rest) which is working fine

The problem I have is that I don't know how to properly add a new parent line with couple of children records on changing some field of an existing line (in the same Sale Order)

Meaning that if I change the quantity of one line, based on some conditions of course, I need to add another parent line with couple of children lines.

There are actually two problems, combined (examples of code below):

  1. Where exactly to put the code ?

    1. Most logical place would be in `sale_order_line.onchange('product_uom_qty')`
      But here I couldn't add even the single parent line using `self.new()` (it's just not showing up) although I'm on the same model, and using `self.create()` it's working but the new records do not show up until the user saves the Sale Order or just refresh / reloads it (as the records have been created into the DB using create() method)

    2. I did try also on `sale_order.onchange('order_line')`
      Here it's working to add the lines even with `new()` but the problem is that the parent / child relationship it's not held

  2. What method to use ?

    1. `sale_order_line.new()` - I would have preferred not to have the records saved into DB directly (in order to allow user to discard the change) but at least one method to work would be nice ... but this is not keeping the parent / child relationship
      Although if using debugger on the moment they're added, the connection between them seems fine using the NewIDs but as soon as the transaction finishes (I don't know which other code it's executing after my function) the relation drops (on next debug the relationship fields are empty)

    2. `sale_order_line.create()` - this seems to work, adding first the parent line and then connecting each child with the parent record ID, but the records do not show up in the view until user saves the Sale Order or simply refreshes / reloads the page

    3. `sale_order.update({'order_line': [(0, 0, {'product_id': parent, 'my_children_ids': [(0, 0, child products ...)]})` same issues as above


Code examples:

1. using `sale_order_line.new()` inside `sale_order_line._onchange_product_uom_qty()` method

not working at all, even for the single parent line, it doesn't show up ...

# in my SaleOrderLine extension class
@api.onchange('product_uom_qty')
def _onchange_product_uom_qty(self):
new_parent_line = self.new({
'order_id': rec.order_id.id,
'product_id': 10,
# I'm adding all the other required fields just to be sure ...
'name': 'Test parent',
'price_unit': 10,
'product_uom_qty': 1,
})


2. using `sale_order_line.create()` inside `sale_order_line._onchange_product_uom_qty()` method

it's working, even adding child lines correctly, but:

  •  they are not visible to the user until Sale Order get's saved manually by the user (which does some kind of refresh afterwards) or entire page reloaded (as the records are already saved in the DB)
    Is there a method to save the Sale Order from code and automatically show the new records created ?

  • it would have been nice to be able to use the discard functionality (like new()) - but anyway, at least one decent way to achieve what I need would be nice though (correct parent / child relationship and displayed to the user after the change ...)

# in my SaleOrderLine extension class
@api.onchange('product_uom_qty')
def _onchange_product_uom_qty(self):
new_parent_line = self.create({
# I need to use the _origin to take order integer ID instead of the NewID format in order to work correctly ...
'order_id': rec._origin.order_id.id,
'product_id': 10,
# I'm adding all the other required fields just to be sure ...
'name': 'Test parent',
'price_unit': 10,
'product_uom_qty': 1,
# this doesn't work to automatically create children records too ...
# 'my_children_ids': [
# (0, 0, {child 1 values ...}),
# (0, 0, {child 2 values ...}),
# ],
})

new_child_1_line = self.create({
'my_parent_id': new_parent_line.id,
# other child 1 values ...
})


3. using `sale_order.order_line.new()` from `sale_order._onchange_order_line()`

The records show up, but the parent / child relation it's not kept

Actually debugging the code during execution which adds them, the relationship seems fine (based on the NewIDs), but as soon as the code ends (and whatever the framework does afterwards) it seems that is broken

I've debugged again and for those records the link fields (parent / child) are empty

 

# in my SaleOrder extension class
@api.onchange('order_line')
def _onchange_order_line(self):
new_parent_line = self.order_line.new({
'order_id': self.id,
'product_id': 10,
# I'm adding all the other required fields just to be sure ...
'name': 'Test parent',
'price_unit': 10,
'product_uom_qty': 1,
   'my_children_ids': [
(0, 0, {child 1 values ...}),
(0, 0, {child 2 values ...}),
]
})


4. using `sale_order.update({'order_line': [(0, 0, {parent values ...}]})` from `sale_order._omchange_order_line()`

the same like point above, they appear but without parent / child relation

# in my SaleOrder extension class
@api.onchange('order_line')
def _onchange_order_line(self):
self.update({
'order_line': [(0, 0, {
'order_id': self.id,
'product_id': 10,
# I'm adding all the other required fields just to be sure ...
'name': 'Test parent',
'price_unit': 10,
'product_uom_qty': 1,
    'my_children_ids': [
(0, 0, {child 1 values ...}),
(0, 0, {child 2 values ...}),
]
})]
})


Are there any other ways to achieve what I need ?

Thx,

0
Avatar
Descartar
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
Attached PDF file is not formatted properly
v14
Avatar
Avatar
1
de des. 25
412
Odoo14 alternative for Automated Translations through Gengo API module
v14
Avatar
Avatar
Avatar
Avatar
3
de set. 25
3840
Odoo Community v14 Slow on High-End Servers, Fast on i5/i7 PCs
v14
Avatar
0
d’ag. 25
1225
How to Managing Birthdate and Age in Odoo
v14
Avatar
Avatar
1
d’ag. 25
3631
Update Initial Stock of Product using XMLRPC API [Odoo14] Solved
v14
Avatar
Avatar
Avatar
2
de jul. 25
9257
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