Skip to Content
Odoo Menu
  • Sign in
  • Try it free
  • Apps
    Finance
    • Accounting
    • Invoicing
    • Expenses
    • Spreadsheet (BI)
    • Documents
    • Sign
    Sales
    • CRM
    • Sales
    • POS Shop
    • POS Restaurant
    • Subscriptions
    • Rental
    Websites
    • Website Builder
    • eCommerce
    • Blog
    • Forum
    • Live Chat
    • eLearning
    Supply Chain
    • Inventory
    • Manufacturing
    • PLM
    • Purchase
    • Maintenance
    • Quality
    Human Resources
    • Employees
    • Recruitment
    • Time Off
    • Appraisals
    • Referrals
    • Fleet
    Marketing
    • Social Marketing
    • Email Marketing
    • SMS Marketing
    • Events
    • Marketing Automation
    • Surveys
    Services
    • Project
    • Timesheets
    • Field Service
    • Helpdesk
    • Planning
    • Appointments
    Productivity
    • Discuss
    • Approvals
    • IoT
    • VoIP
    • Knowledge
    • WhatsApp
    Third party apps Odoo Studio Odoo Cloud Platform
  • Industries
    Retail
    • Book Store
    • Clothing Store
    • Furniture Store
    • Grocery Store
    • Hardware Store
    • Toy Store
    Food & Hospitality
    • Bar and Pub
    • Restaurant
    • Fast Food
    • Guest House
    • Beverage Distributor
    • Hotel
    Real Estate
    • Real Estate Agency
    • Architecture Firm
    • Construction
    • Estate Management
    • Gardening
    • Property Owner Association
    Consulting
    • Accounting Firm
    • Odoo Partner
    • Marketing Agency
    • Law firm
    • Talent Acquisition
    • Audit & Certification
    Manufacturing
    • Textile
    • Metal
    • Furnitures
    • Food
    • Brewery
    • Corporate Gifts
    Health & Fitness
    • Sports Club
    • Eyewear Store
    • Fitness Center
    • Wellness Practitioners
    • Pharmacy
    • Hair Salon
    Trades
    • Handyman
    • IT Hardware & Support
    • Solar Energy Systems
    • Shoe Maker
    • Cleaning Services
    • HVAC Services
    Others
    • Nonprofit Organization
    • Environmental Agency
    • Billboard Rental
    • Photography
    • Bike Leasing
    • Software Reseller
    Browse all Industries
  • Community
    Learn
    • Tutorials
    • Documentation
    • Certifications
    • Training
    • Blog
    • Podcast
    Empower Education
    • Education Program
    • Scale Up! Business Game
    • Visit Odoo
    Get the Software
    • Download
    • Compare Editions
    • Releases
    Collaborate
    • Github
    • Forum
    • Events
    • Translations
    • Become a Partner
    • Services for Partners
    • Register your Accounting Firm
    Get Services
    • Find a Partner
    • Find an Accountant
    • Meet an advisor
    • Implementation Services
    • Customer References
    • Support
    • Upgrades
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Get a demo
  • Pricing
  • Help

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

  • CRM
  • e-Commerce
  • Accounting
  • Inventory
  • PoS
  • Project
  • MRP
All apps
You need to be registered to interact with the community.
All Posts People Badges
Tags (View all)
odoo accounting v14 pos v15
About this forum
You need to be registered to interact with the community.
All Posts People Badges
Tags (View all)
odoo accounting v14 pos v15
About this forum
Help

Hierarchy relationship

Subscribe

Get notified when there's activity on this post

This question has been flagged
v14
5213 Views
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
Discard
Enjoying the discussion? Don't just read, join in!

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

Sign up
Related Posts Replies Views Activity
Attached PDF file is not formatted properly
v14
Avatar
Avatar
1
Dec 25
404
Odoo14 alternative for Automated Translations through Gengo API module
v14
Avatar
Avatar
Avatar
Avatar
3
Sep 25
3828
Odoo Community v14 Slow on High-End Servers, Fast on i5/i7 PCs
v14
Avatar
0
Aug 25
1223
How to Managing Birthdate and Age in Odoo
v14
Avatar
Avatar
1
Aug 25
3629
Update Initial Stock of Product using XMLRPC API [Odoo14] Solved
v14
Avatar
Avatar
Avatar
2
Jul 25
9253
Community
  • Tutorials
  • Documentation
  • Forum
Open Source
  • Download
  • Github
  • Runbot
  • Translations
Services
  • Odoo.sh Hosting
  • Support
  • Upgrade
  • Custom Developments
  • Education
  • Find an Accountant
  • Find a Partner
  • Become a Partner
About us
  • Our company
  • Brand Assets
  • Contact us
  • Jobs
  • Events
  • Podcast
  • Blog
  • Customers
  • Legal • Privacy
  • Security
الْعَرَبيّة 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