Skip to Content
Odoo Menu
  • Prisijungti
  • Išbandykite nemokamai
  • Programėlės
    Finansai
    • Apskaita
    • Pateikimas apmokėjimui
    • Sąnaudos
    • Skaičiuoklė (BI)
    • Dokumentai
    • Pasirašymas
    Pardavimai
    • CRM
    • Pardavimai
    • Kasų sistema - Parduotuvė
    • Kasų sistema - Restoranas
    • Prenumeratos
    • Nuoma
    Svetainės
    • Svetainių kūrėjimo įrankis
    • El. Prekyba
    • Internetinis Tinklaraštis
    • Forumas
    • Tiesioginis pokalbis
    • eMokymasis
    Tiekimo grandinė
    • Atsarga
    • Gamyba
    • PLM
    • Įsigijimai
    • Priežiūra
    • Kokybė
    Žmogaus ištekliai
    • Darbuotojai
    • Įdarbinimas
    • Atostogos
    • Įvertinimai
    • Rekomendacijos
    • Transporto priemonės
    Rinkodara
    • Socialinė rinkodara
    • Rinkodara el. paštu
    • SMS rinkodara
    • Renginiai
    • Rinkodaros automatizavimas
    • Apklausos
    Paslaugos
    • Projektas
    • Darbo laiko žiniaraščiai
    • Priežiūros tarnyba
    • Pagalbos tarnyba
    • Planavimas
    • Rezervacijos
    Produktyvumas
    • Diskucija
    • Patvirtinimai
    • IoT
    • VoIP
    • Žinių biblioteka
    • WhatsApp
    Trečiųjų šalių programos Odoo Studija Odoo debesijos platforma
  • Pramonės šakos
    Mažmeninė prekyba
    • Knygynas
    • Drabužių parduotuvė
    • Baldų parduotuvė
    • Maisto prekių parduotuvė
    • Techninės įrangos parduotuvė
    • Žaislų parduotuvė
    Food & Hospitality
    • Barai ir pub'ai
    • Restoranas
    • Greitasis maistas
    • Guest House
    • Gėrimų platintojas
    • Hotel
    Real Estate
    • Real Estate Agency
    • Architektūros įmonė
    • Konstrukcija
    • Estate Managament
    • Sodininkauti
    • Turto savininkų asociacija
    Consulting
    • Accounting Firm
    • Odoo Partneris
    • Marketing Agency
    • Teisinė firma
    • Talentų paieška
    • Auditai & sertifikavimas
    Gamyba
    • Textile
    • Metal
    • Furnitures
    • Food
    • Brewery
    • Įmonių dovanos
    Sveikata & Fitnesas
    • Sporto klubas
    • Akinių parduotuvė
    • Fitneso Centras
    • Sveikatos praktikai
    • Vaistinė
    • Kirpėjas
    Trades
    • Handyman
    • IT įranga ir palaikymas
    • Solar Energy Systems
    • Shoe Maker
    • Cleaning Services
    • HVAC Services
    Others
    • Nonprofit Organization
    • Aplinkos agentūra
    • Reklaminių stendų nuoma
    • Fotografavimas
    • Dviračių nuoma
    • Programinės įrangos perpardavėjas
    Browse all Industries
  • Bendrija
    Mokykitės
    • Mokomosios medžiagos
    • Dokumentacija
    • Sertifikatai
    • Mokymai
    • Internetinis Tinklaraštis
    • Tinklalaidės
    Skatinkite švietinimą
    • Švietimo programa
    • Scale Up! Verslo žaidimas
    • Aplankykite Odoo
    Gaukite programinę įrangą
    • Atsisiųsti
    • Palyginkite versijas
    • Leidimai
    Bendradarbiauti
    • Github
    • Forumas
    • Renginiai
    • Vertimai
    • Tapkite partneriu
    • Services for Partners
    • Registruokite jūsų apskaitos įmonę
    Gaukite paslaugas
    • Susiraskite partnerį
    • Susirask buhalterį
    • Susitikti su konsultantu
    • Diegimo paslaugos
    • Klientų rekomendavimas
    • Palaikymas
    • Atnaujinimai
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Gaukite demo
  • Kainodara
  • Pagalba

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

  • CRM
  • e-Commerce
  • Apskaita
  • Atsarga
  • PoS
  • Projektas
  • MRP
All apps
You need to be registered to interact with the community.
All Posts People Badges
Žymos (View all)
odoo accounting v14 pos v15
About this forum
You need to be registered to interact with the community.
All Posts People Badges
Žymos (View all)
odoo accounting v14 pos v15
About this forum
Pagalba

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

Prenumeruoti

Get notified when there's activity on this post

This question has been flagged
ormwritemany2manywebsiteodoo12
3 Replies
61598 Rodiniai
Portretas
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
Portretas
Atmesti
Ibrahim Boudmir

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

Christoph Farnleitner
Autorius

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)

Portretas
Michael Jurke
Best Answer


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
Portretas
Atmesti
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

Portretas
Sudhir Arya (ERP Harbor Consulting Services)
Best Answer

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
Portretas
Atmesti
Christoph Farnleitner
Autorius

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
Autorius

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...

Portretas
Oscar del Rio
Best Answer

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
Portretas
Atmesti
Enjoying the discussion? Don't just read, join in!

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

Registracija
Related Posts Replies Rodiniai Veikla
move webpage from one project to another
website odoo12
Portretas
Portretas
Portretas
2
liep. 25
7144
Unpublished website page can't view by Agent without access in website in settings
website odoo12
Portretas
0
rugs. 22
803
Odoo 15 - Can't properly update custom many2many field from website
many2many website
Portretas
0
geg. 22
3188
Odoo12: many2many to a sale.order.line write the wrong ID on save
many2many odoo12
Portretas
0
rugp. 19
3289
How to embed a external calendar in website
website odoo12
Portretas
Portretas
1
saus. 19
5374
Bendrija
  • Mokomosios medžiagos
  • Dokumentacija
  • Forumas
Atvirasis kodas
  • Atsisiųsti
  • Github
  • Runbot
  • Vertimai
Paslaugos
  • Odoo.sh talpinimas
  • Palaikymas
  • Atnaujinti
  • Pritaikytas programavimo kūrimas
  • Švietimas
  • Susirask buhalterį
  • Susiraskite partnerį
  • Tapkite partneriu
Apie mus
  • Mūsų įmonė
  • Prekės ženklo turtas
  • Susisiekite su mumis
  • Darbo pasiūlymai
  • Renginiai
  • Tinklalaidės
  • Internetinis Tinklaraštis
  • Klientai
  • Teisinis • Privatumas
  • Saugumas
الْعَرَبيّة 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 yra atvirojo kodo verslo programų rinkinys, kuris apima visas įmonės poreikius: CRM, El. Prekybą, Apskaitą, Atsargų, Kasų sistemą, Projektų valdymą ir kt.

Unikali Odoo vertės pasiūla – būti tuo pačiu metu labai lengvai naudojama ir visiškai integruota sistema.

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