Skip to Content
Odoo Menu
  • Prijavi
  • 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
  • Knjigovodstvo
  • Zaloga
  • PoS
  • Projekt
  • MRP
All apps
You need to be registered to interact with the community.
All Posts People Badges
Ključne besede (View all)
odoo accounting v14 pos v15
About this forum
You need to be registered to interact with the community.
All Posts People Badges
Ključne besede (View all)
odoo accounting v14 pos v15
About this forum
Pomoč

write() vs update()

Naroči se

Get notified when there's activity on this post

This question has been flagged
apirecordsetORM
4 Odgovori
43826 Prikazi
Avatar
Cayprol

Hello,


Could anyone explain how ORM API works with write() and update().  

AFAIK, CRUD represents create, read, update, delete which corresponds to create(), read(), update(), unlink() in ORM.

However, the explanation on odoo 13.0 documentation (\https://www.odoo.com/documentation/13.0/reference/orm.html)

write() seems to be the one to update a existing record.  

update() can do the same, and even on psedo-records that are not created yet.  

If that is the case, what are the scenarios to use write() over update()?

0
Avatar
Opusti
christian fotie

Hi Guys
Please can have a example of using write() and update() ?
odoo 13

Avatar
Cybrosys Techno Solutions Pvt.Ltd
Best Answer

Hi,

Write() Updates all records in the current set with the provided values, but write() is one of the CRUD methods that do not work for records set which are not present in the database. An onchange method returns pseudo-records which do not exist in the database yet. So set record's field using update() method as calling write() method gives an undefined behavior.

Regards

6
Avatar
Opusti
Cayprol
Avtor

If I understand you correctly.

1. CRUD, is write(), not update(), odoo ORM naming the way it is.

2. write() can update multiple records and its corresponding fields at the same time.

3. update() is designed to work with pseudo-records.

The only benefit of write() is the ability to update multiple records. Otherwise use update() in custom function?

I make this conclusion because update() can update existed records as well. I've not tried update() multiple yet though.

Cybrosys Techno Solutions Pvt.Ltd

When setting multiple fields at the same time use write() so that if it takes a number of field values the function writes them to all the records in its recordset. self.write({'name':'Name', 'age': age})

So gives you single update statement in the database.

But update() only works on singletons and performs a database write for each written field.So at a time it updates only single record.It Iterates through the fields and sets each one separately giving you multiple update statements in the database.

for order in self:

amount_untaxed = amount_tax = 0.0

for line in order.order_line:

amount_untaxed += line.price_subtotal

amount_tax += line.price_tax

order.update({

'amount_untaxed': amount_untaxed,

'amount_tax': amount_tax,

'amount_total': amount_untaxed + amount_tax,

})

and it can be used within onchange functions as they return pseudo records.ie.update function can work on records not present in database yet.

Cayprol
Avtor

Thank you very much for the clears up.

I was doing it wrong by abusing for-loop with update().

Avatar
Falguni Tank
Best Answer

Hello
in write method , all the record has been updated of model and in update method, only particular filed of record you can update. This is the main difference between write and update method.

0
Avatar
Opusti
Cayprol
Avtor

thank you for the explanation.

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

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

Prijavi
Related Posts Odgovori Prikazi Aktivnost
Odoo v12 when model changed without the UI event
api Oddov11 ORM
Avatar
Avatar
Avatar
2
nov. 19
4077
How to obtains a list of id instead of a recordset with the search method in the new API? Solved
api list new recordset
Avatar
Avatar
3
apr. 17
19220
How to put twice the same record in a recordset ? Solved
api record v9 recordset
Avatar
1
feb. 16
4754
Has anyone integrated Helpdesk with Zoom for meeting scheduling?
api
Avatar
Avatar
1
avg. 25
1239
Using API check if Odoo is using Odoo sh or on-premises hosting. Solved
api
Avatar
Avatar
1
avg. 25
1597
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