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
    • Maison d’hôtes
    • Distributeur de boissons
    • Hôtel
    Immobilier
    • Agence immobilière
    • Cabinet d'architecture
    • Construction
    • Gestion immobilière
    • Jardinage
    • Association de copropriétaires
    Consultance
    • Cabinet d'expertise comptable
    • Partenaire Odoo
    • Agence Marketing
    • Cabinet d'avocats
    • Aquisition de talents
    • Audit & Certification
    Fabrication
    • Textile
    • Métal
    • Meubles
    • Alimentation
    • 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
    • Systèmes photovoltaïques
    • Cordonnier
    • Services de nettoyage
    • Services CVC
    Autres
    • Organisation à but non lucratif
    • 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

many2many write() - replaces all existing records in the set by the ids

S'inscrire

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

Cette question a été signalée
ormwritemany2manywebsiteodoo12
3 Réponses
61768 Vues
Avatar
Christoph Farnleitner

I'm trying to update a many2many field from website via a <select multiple> element.

However, I'm unable to store already existing ids along with newly selected options.


For example, the many2many relation holds the ids 1,2,3. Now, upon updating the new ids would supposed to be 1,2,3,4,5 based on the selected <option>s in the <select> - but only the new ids (4,5) are stored, 1,2,3 get removed.


values['field'] = (6, 0, [1,2,3,4,5])
res.write(values)


Any hints, why this could be happening? Maybe I'm misinterpreting the documentation saying "(6, _, ids)

replaces all existing records in the set by the ids list, equivalent to using the command 5 followed by a command 4 for each id in ids." (https://www.odoo.com/documentation/12.0/reference/orm.html#model-reference)?


---

Edit fyi:

Model:

_inherit = 'res.partner'
field = fields.Many2many(comodel_name='res.country', relation="some_country_res_partner_rel", string='Some countries', default=False)



3
Avatar
Ignorer
Ibrahim Boudmir

values['field'] = [(6, 0, [1,2,3,4,5])]

Christoph Farnleitner
Auteur

Nope, "values" (at the time of "res.write(values)") is {'field': (6, 0, ['1', '2','3','4','5'])} - your suggestion would get me {'field': ([(6, 0, ['1', '2','3','4','5'])],)} which then does not update the many2many field at all (already set relations are kept in that case though)

Avatar
Michael Jurke
Meilleure réponse


Update 2022, Odoo 15.0 and later. You can use the new Command-helper API.

  • (0, 0, { values }) -- Command.create({...})
  • (1, ID, { values }) -- Command.update({...})
  • (2, ID) -- Command.delete(...)
  • (3, ID) -- Command.unlink(...)
  • (4, ID) -- Command.link(...)
  • (5) -- Command.clear()
  • (6, 0, [IDs]) -- Command.set([...])

The Command-helpers will create the typles with the right number from record ids or values, e.g.

my_obj.write({'ur_m2m_field': [Command.link(id)]})


4
Avatar
Ignorer
Ralf Müller

https://www.odoo.com/documentation/15.0/developer/reference/backend/orm.html#odoo.fields.Command
https://www.odoo.com/documentation/15.0/developer/reference/backend/orm.html#odoo.models.Model.write

Avatar
Sudhir Arya (ERP Harbor Consulting Services)
Meilleure réponse

You should use (4, id) in the write method.

Ex:

for id in [4,5]:
my_obj.write({'ur_m2m_field': [(4, id)]})

There are actually0-6 numbers for representing each job for a many2many/ one2many field

  • (0, 0, { values }) -- link to a new record that needs to be created with the given values dictionary
  • (1, ID, { values }) -- update the linked record with id = ID (write values on it)
  • (2, ID) -- remove and delete the linked record with id = ID (calls unlink on ID, that will delete the object completely, and the link to it as well)
  • (3, ID) -- cut the link to the linked record with id = ID (delete the relationship between the two objects but does not delete the target object itself)
  • (4, ID) -- link to existing record with id = ID (adds a relationship)
  • (5) -- unlink all (like using (3,ID) for all linked records)
  • (6, 0, [IDs]) -- replace the list of linked IDs (like using (5) then (4,ID) for each ID in the list of IDs)
16
Avatar
Ignorer
Christoph Farnleitner
Auteur

Yes, in fact this would store ADDITIONAL relations, but what if one deselects something from <select multiple> (assume the m2m field holds currently ids 1,2 - now one wants to set 1,3,4; in your example i would end up with 1,2,3,4). Judging by the documentation one would probably do (5,_,_) and then (4,id) - which in return should be equal to (6,_,ids) [at least based on the documentation..?] - or can you think about another way of making sure that all actually selected options get stored -- or am I completely mistaken now?

Christoph Farnleitner
Auteur

had some further tries:

<select multiple> --> changing from no selection to Afghanistan

{'field': [(5, 0, 0), (4, '3')]} --> will be stored

Saving the same form again (Afghanistan still selected)

{'field': [(5, 0, 0), (4, '3')]} --> will not be stored anylonger - so it behaves pretty much like my initial try with (6,0,ids) for some reason...

Avatar
Oscar del Rio
Meilleure réponse

Hi, I am using Odoo 14 and Sudhir answer works, but it seems that Odoo is protecting itself because it gets back in a second to the original values. I am trying to link an invoice to as sale order, so I am using:

record.write({'invoice_ids': [(3, 456)]}) #456 is the record of the invoice
raise Warning(record.invoice_ids)

The warning shows that the new Id has been appended to the current values, but if I check again in the next second, the values get back to the original. Any clue on how to make them persistent?

Thx.


0
Avatar
Ignorer
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é
move webpage from one project to another
website odoo12
Avatar
Avatar
Avatar
2
juil. 25
7229
Unpublished website page can't view by Agent without access in website in settings
website odoo12
Avatar
0
sept. 22
803
Odoo 15 - Can't properly update custom many2many field from website
many2many website
Avatar
0
mai 22
3238
Odoo12: many2many to a sale.order.line write the wrong ID on save
many2many odoo12
Avatar
0
août 19
3378
How to embed a external calendar in website
website odoo12
Avatar
Avatar
1
janv. 19
5451
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