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

ODOO 8.0 @api.onchange problem

Subscribe

Get notified when there's activity on this post

This question has been flagged
onchange8.0
2 Replies
5069 Views
Avatar
Jorge Barcelo Cortes

Hello. Before start some code:

class ExampleModelItem(models.Model):
_name = "example.model.item"
partner_id = fields.Many2one(comodel_name='res.partner')
lines = fields.Many2many(comodel_name='sale.order.line')

@api.onchange("partner_id")
def _compute_sale_lines(self):
ctx_obj = self._context['reglas']

class ExampleModel(models.Model):
_name = "example.model"
reglas = fields.One2many(comodel_name='example.model.item', inverse_name='...', string="...")
lines = fields.Many2many(comodel_name='sale.order.line')

The view in "example.model" looks like :


<field name="reglas" context="{'reglas': reglas}">
<tree>...</tree>
<form>...</form>
</field>

As you can see in the view we set the field in the context, we get the value to ctx_obj variable in the _compute function.

This ctx_obj have a list of the old o2m structure [(0, False, {...}), (1, 1, {...}), (4, 3, False), ..., (type, id, data)]

The problem i facing is, how can i know in the compute method if the record is one of the records in the ctx_obj? Or even better, wich one. When self.id exists its easy, because i can find that id in the ctx_obj. But when self.id is a Newid object, the ctx_obj will be a False. Is there a way to acknowledge is the newid is already in the context object or not??


Thank you in advance

0
Avatar
Discard
Avatar
Jorge Barcelo Cortes
Author Best Answer

The problem is not o get values to the function. As you can see the data reach the function.

Let me elaborate it a little more:

Beeing in the main form, i have values {'reglas': [], 'lines': [(6, 0, [100201, 100202])]}

No i create a new subform, and when the function is called i have ctx_obj=[]. The function runs, data is build and back to the main form with values {'reglas': [(0, False, {'partner_id': 38, 'lines': [(6, 0, [100201])]})]}.

Now if i edit again the same item before i save the main form, and change partner_id....ctx_obj=[(0, False, {'partner_id': 38, 'lines': [(6, 0, [100201])]})], and the record doesnt have an ID.

If instead of edit, i create a second one, ctx_obj is the same. The main point of this post is, how can i relate the record to the item inside ctx_obj??

For now a strinct rule of "not edit not saved data" work, but i would like to remove the problem before impose such thing.

0
Avatar
Discard
Avatar
Deepa Venkatesh
Best Answer

Context must be specified on the source-field of the onchange.

In your case, ensure whether you are passing the necessary context on the field 'partner_id'

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
@api.onchange causes looping page redirect 302 Solved
onchange 8.0
Avatar
Avatar
1
Mar 15
6028
@api.onchange strange behavior in Odoo 8.0 Solved
onchange 8.0
Avatar
Avatar
Avatar
3
Mar 15
5074
trying to auto correct a wrong users value
onchange
Avatar
Avatar
1
Oct 23
2770
"Wrong value for %s: %r" % (self, value) Solved
onchange
Avatar
Avatar
2
Oct 23
2985
How to add domain in onchange function for a One2many field Solved
onchange
Avatar
Avatar
Avatar
2
Aug 23
5221
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