Skip to Content
Odoo Menú
  • Registra entrada
  • Prova-ho gratis
  • Aplicacions
    Finances
    • Comptabilitat
    • Facturació
    • Despeses
    • Full de càlcul (IA)
    • Documents
    • Signatura
    Vendes
    • CRM
    • Vendes
    • Punt de venda per a botigues
    • Punt de venda per a restaurants
    • Subscripcions
    • Lloguer
    Imatges de llocs web
    • Creació de llocs web
    • Comerç electrònic
    • Blog
    • Fòrum
    • Xat en directe
    • Aprenentatge en línia
    Cadena de subministrament
    • Inventari
    • Fabricació
    • PLM
    • Compres
    • Manteniment
    • Qualitat
    Recursos humans
    • Empleats
    • Reclutament
    • Absències
    • Avaluacions
    • Recomanacions
    • Flota
    Màrqueting
    • Màrqueting Social
    • Màrqueting per correu electrònic
    • Màrqueting per SMS
    • Esdeveniments
    • Automatització del màrqueting
    • Enquestes
    Serveis
    • Projectes
    • Fulls d'hores
    • Servei de camp
    • Suport
    • Planificació
    • Cites
    Productivitat
    • Converses
    • Validacions
    • IoT
    • VoIP
    • Coneixements
    • WhatsApp
    Aplicacions de tercers Odoo Studio Plataforma d'Odoo al núvol
  • Sectors
    Comerç al detall
    • Llibreria
    • Botiga de roba
    • Botiga de mobles
    • Botiga d'ultramarins
    • Ferreteria
    • Botiga de joguines
    Food & Hospitality
    • Bar i pub
    • Restaurant
    • Menjar ràpid
    • Guest House
    • Distribuïdor de begudes
    • Hotel
    Real Estate
    • Real Estate Agency
    • Estudi d'arquitectura
    • Construcció
    • Gestió immobiliària
    • Jardineria
    • Associació de propietaris de béns immobles
    Consulting
    • Accounting Firm
    • Partner d'Odoo
    • Agència de màrqueting
    • Bufet d'advocats
    • Captació de talent
    • Auditoria i certificació
    Fabricació
    • Textile
    • Metal
    • Furnitures
    • Food
    • Brewery
    • Regals corporatius
    Salut i fitness
    • Club d'esport
    • Òptica
    • Centre de fitness
    • Especialistes en benestar
    • Farmàcia
    • Perruqueria
    Trades
    • Servei de manteniment
    • Hardware i suport informàtic
    • Solar Energy Systems
    • Shoe Maker
    • Serveis de neteja
    • HVAC Services
    Others
    • Nonprofit Organization
    • Agència del medi ambient
    • Lloguer de panells publicitaris
    • Fotografia
    • Lloguer de bicicletes
    • Distribuïdors de programari
    Browse all Industries
  • Comunitat
    Aprèn
    • Tutorials
    • Documentació
    • Certificacions
    • Formació
    • Blog
    • Pòdcast
    Potenciar l'educació
    • Programa educatiu
    • Scale-Up! El joc empresarial
    • Visita Odoo
    Obtindre el programari
    • Descarregar
    • Comparar edicions
    • Novetats de les versions
    Col·laborar
    • GitHub
    • Fòrum
    • Esdeveniments
    • Traduccions
    • Converteix-te en partner
    • Services for Partners
    • Registra la teva empresa comptable
    Obtindre els serveis
    • Troba un partner
    • Troba un comptable
    • Contacta amb un expert
    • Serveis d'implementació
    • Referències del client
    • Suport
    • Actualitzacions
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Programar una demo
  • Preus
  • Ajuda

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

  • CRM
  • e-Commerce
  • Comptabilitat
  • Inventari
  • PoS
  • Projectes
  • MRP
All apps
You need to be registered to interact with the community.
All Posts People Badges
Etiquetes (View all)
odoo accounting v14 pos v15
About this forum
You need to be registered to interact with the community.
All Posts People Badges
Etiquetes (View all)
odoo accounting v14 pos v15
About this forum
Ajuda

.write() behaviour on Many2Many - Odoo 10

Subscriure's

Get notified when there's activity on this post

This question has been flagged
tagswritemany2many
2 Respostes
4171 Vistes
Avatar
Randy Risser

My question is:
I am automatically tagging customers depending on the analytic account from sales orders. Does a .write() always write on the record even when the value is already there? Should I be doing a check with an if statement to see if the tag has already been set? If so, how do I check that? I tried a few things and couldn't figure it out.

*Edit: I should have noted that my code is working well but I'm just wondering if I should be checking if the category_id value is already there before the write happens and if so how to check. I am still very much in the  learning stage of Odoo development*

My code:

contact_tag_id = None
project_id = record.project_id.id
project_id_name = record.project_id.name
partner_id = record.partner_id.commercial_partner_id.id

if project_id == 44:
        contact_tag_id = 4
if project_id == 49:
        contact_tag_id = 6
if project_id == 48:
        contact_tag_id = 3
#Add the tag to contact
if contact_tag_id != None:
        project_id = contact_tag_id
        env['res.partner'].search([('id','=',partner_id)]).sudo().write({'category_id': [(4, contact_tag_id)]})

0
Avatar
Descartar
DHA Medic

i haven't known your model and type of variable, so i can't fix your code

Avatar
subbarao
Best Answer

If the category_id is many2many field on partner master, then you can try the following code.

partner_ids = self.env['res.partner'].search([('id','=',partner_id)])

if partner_ids:

    category_ids = [contact_tag_id]+partner_ids.category_id.ids

    partner_ids.sudo().write({'category_id': [(6, 0, category_ids)]})

1
Avatar
Descartar
Randy Risser
Autor

Thanks for your reply. See my edit to the question. My code is already working well from the start but how do I check if the ID that I am adding to category_id is already there before the write happens?

subbarao

partner_ids.category_id.ids

Randy Risser
Autor

So something like this?

partner_ids = self.env['res.partner'].search([('id','=',partner_id)])

if contact_tag_id not in partner_ids.category_id.ids

#Add the tag to contact

if contact_tag_id != None:

project_id = contact_tag_id

env['res.partner'].search([('id','=',partner_id)]).sudo().write({'category_id': [(4, contact_tag_id)]})

subbarao

yes your right

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

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

Registrar-se
Related Posts Respostes Vistes Activitat
How to make employee_ids with value of Employee - Department. Solved
tags many2many v14
Avatar
Avatar
1
de set. 23
2695
[closed] help with xmlrpc + many2many_tags
tags xmlrpc many2many
Avatar
Avatar
2
de març 15
5732
Get values from one2many / many2many fields in search filter
tags one2many many2many mass_mailing
Avatar
0
de des. 21
2855
export problem with tags in contact view --> category_id only with one line instead of multiple lines (many2many fields)
export tags many2many many2many_tags
Avatar
Avatar
1
de jul. 20
6218
Is it possible to write data into m2m with the rpc api?
write many2many rpc odoo10
Avatar
Avatar
1
de gen. 19
4672
Community
  • Tutorials
  • Documentació
  • Fòrum
Codi obert
  • Descarregar
  • GitHub
  • Runbot
  • Traduccions
Serveis
  • Allotjament a Odoo.sh
  • Suport
  • Actualització
  • Desenvolupaments personalitzats
  • Educació
  • Troba un comptable
  • Troba un partner
  • Converteix-te en partner
Sobre nosaltres
  • La nostra empresa
  • Actius de marca
  • Contacta amb nosaltres
  • Llocs de treball
  • Esdeveniments
  • Pòdcast
  • Blog
  • Clients
  • Informació legal • Privacitat
  • Seguretat
الْعَرَبيّة 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 és un conjunt d'aplicacions empresarials de codi obert que cobreix totes les necessitats de la teva empresa: CRM, comerç electrònic, comptabilitat, inventari, punt de venda, gestió de projectes, etc.

La proposta única de valor d'Odoo és ser molt fàcil d'utilitzar i estar totalment integrat, ambdues alhora.

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