Skip to Content
Odoo Menu
  • Prihlásiť sa
  • Vyskúšajte zadarmo
  • Aplikácie
    Financie
    • Účtovníctvo
    • Fakturácia
    • Výdavky
    • Tabuľka (BI)
    • Dokumenty
    • Podpis
    Predaj
    • CRM
    • Predaj
    • POS Shop
    • POS Restaurant
    • Manažment odberu
    • Požičovňa
    Webstránky
    • Tvorca webstránok
    • eShop
    • Blog
    • Fórum
    • Živý chat
    • eLearning
    Supply Chain
    • Sklad
    • Výroba
    • Správa životného cyklu produktu
    • Nákup
    • Údržba
    • Manažment kvality
    Ľudské zdroje
    • Zamestnanci
    • Nábor zamestnancov
    • Voľné dni
    • Hodnotenia
    • Odporúčania
    • Vozový park
    Marketing
    • Marketing sociálnych sietí
    • Email marketing
    • SMS marketing
    • Eventy
    • Marketingová automatizácia
    • Prieskumy
    Služby
    • Projektové riadenie
    • Pracovné výkazy
    • Práca v teréne
    • Helpdesk
    • Plánovanie
    • Schôdzky
    Produktivita
    • Tímová komunikácia
    • Schvalovania
    • IoT
    • VoIP
    • Znalosti
    • 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 Managament
    • Gardening
    • Property Owner Association
    Consulting
    • Accounting Firm
    • Odoo Partner
    • Marketing Agency
    • Law firm
    • Talent Acquisition
    • Audit & Certification
    Výroba
    • Textile
    • Metal
    • Furnitures
    • Food
    • Brewery
    • Corporate Gifts
    Health & Fitness
    • Sports Club
    • Eyewear Store
    • Fitness Center
    • Wellness Practitioners
    • Pharmacy
    • Hair Salon
    Trades
    • Handyman
    • IT Hardware and 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
  • Komunita
    Vzdelávanie
    • Tutoriály
    • Dokumentácia
    • Certifikácie
    • Školenie
    • Blog
    • Podcast
    Empower Education
    • Vzdelávací program
    • Scale Up! Business Game
    • Visit Odoo
    Softvér
    • Stiahnuť
    • Porovnanie Community a Enterprise vierzie
    • Releases
    Spolupráca
    • Github
    • Fórum
    • Eventy
    • Preklady
    • Staň sa partnerom
    • Services for Partners
    • Register your Accounting Firm
    Služby
    • Nájdite partnera
    • Nájdite účtovníka
    • Meet an advisor
    • Implementation Services
    • Zákaznícke referencie
    • Podpora
    • Upgrades
    ​Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Získajte demo
  • Cenník
  • Help

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

  • CRM
  • e-Commerce
  • Účtovníctvo
  • Sklady
  • PoS
  • Projektové riadenie
  • MRP
All apps
You need to be registered to interact with the community.
All Posts People Badges
Tagy (View all)
odoo accounting v14 pos v15
About this forum
You need to be registered to interact with the community.
All Posts People Badges
Tagy (View all)
odoo accounting v14 pos v15
About this forum
Pomoc

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

Odoberať

Get notified when there's activity on this post

This question has been flagged
ormwritemany2manywebsiteodoo12
3 Replies
61672 Zobrazenia
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
Zrušiť
Ibrahim Boudmir

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

Christoph Farnleitner
Autor

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
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
Avatar
Zrušiť
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)
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
Avatar
Zrušiť
Christoph Farnleitner
Autor

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
Autor

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
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
Avatar
Zrušiť
Enjoying the discussion? Don't just read, join in!

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

Registrácia
Related Posts Replies Zobrazenia Aktivita
move webpage from one project to another
website odoo12
Avatar
Avatar
Avatar
2
júl 25
7172
Unpublished website page can't view by Agent without access in website in settings
website odoo12
Avatar
0
sep 22
803
Odoo 15 - Can't properly update custom many2many field from website
many2many website
Avatar
0
máj 22
3227
Odoo12: many2many to a sale.order.line write the wrong ID on save
many2many odoo12
Avatar
0
aug 19
3341
How to embed a external calendar in website
website odoo12
Avatar
Avatar
1
jan 19
5398
Komunita
  • Tutoriály
  • Dokumentácia
  • Fórum
Open Source
  • Stiahnuť
  • Github
  • Runbot
  • Preklady
Služby
  • Odoo.sh hosting
  • Podpora
  • Vyššia verzia
  • Custom Developments
  • Vzdelávanie
  • Nájdite účtovníka
  • Nájdite partnera
  • Staň sa partnerom
O nás
  • Naša spoločnosť
  • Majetok značky
  • Kontaktujte nás
  • Pracovné ponuky
  • Eventy
  • Podcast
  • Blog
  • Zákazníci
  • Právne dokumenty • Súkromie
  • Bezpečnosť
الْعَرَبيّة 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 je sada podnikových aplikácií s otvoreným zdrojovým kódom, ktoré pokrývajú všetky potreby vašej spoločnosti: CRM, e-shop, účtovníctvo, skladové hospodárstvo, miesto predaja, projektový manažment atď.

Odoo prináša vysokú pridanú hodnotu v jednoduchom použití a súčasne plne integrovanými biznis aplikáciami.

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