Se rendre au contenu
Odoo Menu
  • Se connecter
  • Essai gratuit
  • Applications
    Finance
    • Comptabilité
    • Facturation
    • Notes de frais
    • Feuilles de calcul (BI)
    • Documents
    • Signature
    Ventes
    • CRM
    • Ventes
    • PdV Boutique
    • PdV Restaurant
    • Abonnements
    • Location
    Sites web
    • Site Web
    • eCommerce
    • Blog
    • Forum
    • Live Chat
    • eLearning
    Chaîne d'approvisionnement
    • Inventaire
    • Fabrication
    • PLM
    • Achats
    • Maintenance
    • Qualité
    Ressources Humaines
    • Employés
    • Recrutement
    • Congés
    • Évaluations
    • Recommandations
    • Parc automobile
    Marketing
    • Marketing Social
    • E-mail Marketing
    • SMS Marketing
    • Événements
    • Marketing Automation
    • Sondages
    Services
    • Projet
    • Feuilles de temps
    • Services sur Site
    • Assistance
    • Planification
    • Rendez-vous
    Productivité
    • Discussion
    • Validations
    • Internet des Objets
    • VoIP
    • Connaissances
    • WhatsApp
    Applications tierces Odoo Studio Plateforme Cloud d'Odoo
  • Industries
    Commerce de détail
    • Librairie
    • Magasin de vêtements
    • Magasin de meubles
    • Épicerie
    • Quincaillerie
    • Magasin de jouets
    Food & Hospitality
    • Bar et Pub
    • Restaurant
    • Fast-food
    • Guest House
    • Distributeur de boissons
    • Hotel
    Real Estate
    • Real Estate Agency
    • Cabinet d'architecture
    • Construction
    • Gestion immobilière
    • Jardinage
    • Association de copropriétaires
    Consulting
    • Accounting Firm
    • Partenaire Odoo
    • Agence Marketing
    • Cabinet d'avocats
    • Aquisition de talents
    • Audit & Certification
    Fabrication
    • Textile
    • Metal
    • Furnitures
    • Food
    • Brewery
    • Cadeaux d'entreprise
    Santé & Fitness
    • Club de sports
    • Opticien
    • Salle de fitness
    • Praticiens bien-être
    • Pharmacie
    • Salon de coiffure
    Trades
    • Bricoleur
    • Matériel informatique et support
    • Solar Energy Systems
    • Cordonnier
    • Services de nettoyage
    • HVAC Services
    Others
    • Nonprofit Organization
    • Agence environnementale
    • Location de panneaux d'affichage
    • Photographie
    • Leasing de vélos
    • Revendeur de logiciel
    Browse all Industries
  • Communauté
    Apprenez
    • Tutoriels
    • Documentation
    • Certifications
    • Formation
    • Blog
    • Podcast
    Renforcer l'éducation
    • Programme éducatif
    • Business Game Scale-Up!
    • Rendez-nous visite
    Obtenir le logiciel
    • Téléchargement
    • Comparez les éditions
    • Versions
    Collaborer
    • Github
    • Forum
    • Événements
    • Traductions
    • Devenez partenaire
    • Services for Partners
    • Enregistrer votre cabinet comptable
    Nos Services
    • Trouver un partenaire
    • Trouver un comptable
    • Rencontrer un conseiller
    • Services de mise en œuvre
    • Références clients
    • Assistance
    • Mises à niveau
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Obtenir une démonstration
  • Tarification
  • Aide

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

  • CRM
  • e-Commerce
  • Comptabilité
  • Inventaire
  • PoS
  • Projet
  • MRP
All apps
Vous devez être inscrit pour interagir avec la communauté.
Toutes les publications Personnes Badges
Étiquettes (Voir toutl)
odoo accounting v14 pos v15
À propos de ce forum
Vous devez être inscrit pour interagir avec la communauté.
Toutes les publications Personnes Badges
Étiquettes (Voir toutl)
odoo accounting v14 pos v15
À propos de ce forum
Aide

write() vs update()

S'inscrire

Recevez une notification lorsqu'il y a de l'activité sur ce poste

Cette question a été signalée
apirecordsetORM
4 Réponses
43804 Vues
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
Ignorer
christian fotie

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

Avatar
Cybrosys Techno Solutions Pvt.Ltd
Meilleure réponse

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
Ignorer
Cayprol
Auteur

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
Auteur

Thank you very much for the clears up.

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

Avatar
Falguni Tank
Meilleure réponse

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
Ignorer
Cayprol
Auteur

thank you for the explanation.

Vous appréciez la discussion ? Ne vous contentez pas de lire, rejoignez-nous !

Créez un compte dès aujourd'hui pour profiter de fonctionnalités exclusives et échanger avec notre formidable communauté !

S'inscrire
Publications associées Réponses Vues Activité
Odoo v12 when model changed without the UI event
api Oddov11 ORM
Avatar
Avatar
Avatar
2
nov. 19
4062
How to obtains a list of id instead of a recordset with the search method in the new API? Résolu
api list new recordset
Avatar
Avatar
3
avr. 17
19214
How to put twice the same record in a recordset ? Résolu
api record v9 recordset
Avatar
1
févr. 16
4743
Has anyone integrated Helpdesk with Zoom for meeting scheduling?
api
Avatar
Avatar
1
août 25
1225
Using API check if Odoo is using Odoo sh or on-premises hosting. Résolu
api
Avatar
Avatar
1
août 25
1579
Communauté
  • Tutoriels
  • Documentation
  • Forum
Open Source
  • Téléchargement
  • Github
  • Runbot
  • Traductions
Services
  • Hébergement Odoo.sh
  • Assistance
  • Migration
  • Développements personnalisés
  • Éducation
  • Trouver un comptable
  • Trouver un partenaire
  • Devenez partenaire
À propos
  • Notre société
  • Actifs de la marque
  • Contactez-nous
  • Emplois
  • Événements
  • Podcast
  • Blog
  • Clients
  • Informations légales • Confidentialité
  • Sécurité.
الْعَرَبيّة 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 est une suite d'applications open source couvrant tous les besoins de votre entreprise : CRM, eCommerce, Comptabilité, Inventaire, Point de Vente, Gestion de Projet, etc.

Le positionnement unique d'Odoo est d'être à la fois très facile à utiliser et totalement intégré.

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