Skip to Content
Odoo Meniu
  • Autentificare
  • Try it free
  • Aplicații
    Finanțe
    • Contabilitate
    • Facturare
    • Cheltuieli
    • Spreadsheet (BI)
    • Documente
    • Semn
    Vânzări
    • CRM
    • Vânzări
    • POS Shop
    • POS Restaurant
    • Abonamente
    • Închiriere
    Site-uri web
    • Constructor de site-uri
    • eCommerce
    • Blog
    • Forum
    • Live Chat
    • eLearning
    Lanț Aprovizionare
    • Inventar
    • Producție
    • PLM
    • Achiziție
    • Maintenance
    • Calitate
    Resurse Umane
    • Angajați
    • Recrutare
    • Time Off
    • Evaluări
    • Referințe
    • Flotă
    Marketing
    • Social Marketing
    • Marketing prin email
    • SMS Marketing
    • Evenimente
    • Automatizare marketing
    • Sondaje
    Servicii
    • Proiect
    • Foi de pontaj
    • Servicii de teren
    • Centru de asistență
    • Planificare
    • Programări
    Productivitate
    • Discuss
    • Aprobări
    • IoT
    • VoIP
    • Knowledge
    • WhatsApp
    Aplicații Terțe Odoo Studio Platforma Odoo Cloud
  • Industrii
    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
    Producție
    • 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
  • Comunitate
    Învăță
    • Tutorials
    • Documentație
    • Certificări
    • Instruire
    • Blog
    • Podcast
    Empower Education
    • Program Educațional
    • Scale Up! Business Game
    • Visit Odoo
    Obține Software-ul
    • Descărcare
    • Compară Edițiile
    • Lansări
    Colaborați
    • Github
    • Forum
    • Evenimente
    • Translations
    • Devino Partener
    • Services for Partners
    • Înregistrează-ți Firma de Contabilitate
    Obține Servicii
    • Găsește un Partener
    • Găsiți un contabil
    • Meet an advisor
    • Servicii de Implementare
    • Referințe ale clienților
    • Suport
    • Actualizări
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Obține un demo
  • Prețuri
  • Ajutor

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

  • CRM
  • e-Commerce
  • Contabilitate
  • Inventar
  • PoS
  • Proiect
  • MRP
All apps
Trebuie să fiți înregistrat pentru a interacționa cu comunitatea.
All Posts Oameni Insigne
Etichete (View all)
odoo accounting v14 pos v15
Despre acest forum
Trebuie să fiți înregistrat pentru a interacționa cu comunitatea.
All Posts Oameni Insigne
Etichete (View all)
odoo accounting v14 pos v15
Despre acest forum
Suport

odoo 16 One2many insert value doesnt work

Abonare

Primiți o notificare când există activitate la acestă postare

Această întrebare a fost marcată
one2manyvalueaddodoo16features
2 Răspunsuri
3546 Vizualizări
Imagine profil
Klaus

hi all,

i created a One2many relation like this

line_ids = fields.One2many('comodel', 'relation_id', string = "something")


in the past e.g. odoo 10 i could add a value from code in the One2many field by doing:

value = [(0,0, {'fieldname': something})]

self.update({

    'line_ids' : value,

})

the updated line_ids shows the value imidiately. 

in Odoo 16 i only see in the form that it has to be saved, nothing else.
I can add a value from the form itself, but not from code

in Odo 16 it doesnt seem possible anymore. did they change the way adding values in One2many fields?

hope you understand what i mean. 

thx for any help

0
Imagine profil
Abandonează
Imagine profil
Sayed Mohammed Aqeel Ebrahim
Cel mai bun răspuns

In Odoo 16 and onwards, the mechanism for adding records to One2many fields via code has indeed changed slightly. Instead of directly using `self.update()` to modify One2many fields, you can use the recordset methods like `create` or `write`.


Here's how you can add records to a One2many field in Odoo 16+:


# Assuming self is a record of the main model where line_ids is a One2many field

# Create a recordset for the related model 'comodel'
comodel_obj = self.env['comodel']

# Prepare the values to be added to the One2many field
values_to_add = {
    'fieldname': something,  # Replace 'fieldname' and something with your actual field and value
    # Add other fields and values as needed
}

# Create a new record in 'comodel' and link it to the main record
new_record = comodel_obj.create(values_to_add)

# Add the newly created record to the One2many field 'line_ids'
self.write({
    'line_ids': [(4, new_record.id)]
})



This snippet demonstrates how to create a new record in the related model ('comodel') and then link it to the main record by appending its ID to the One2many field 'line_ids' using the `(4, id)` syntax.


Ensure to replace `'fieldname'` and `something` with your actual field name and value that you want to set in the 'comodel'.


This should enable you to add records to the One2many field programmatically in Odoo 16 and newer versions.

0
Imagine profil
Abandonează
Klaus
Autor

wow, thx so much Sayed Mohammed Aqeel Ebrahim for your answer. thats what i thought and found nothing in internet, or possible i searched not enough. :-)

I was looking for a resolution for days now. in the odoo versions lower then 15 it always worked.
thx a lot i will try this tomorrow

Klaus
Autor

hello Sayed Mohammed Aqeel Ebrahim,
i tried it and it works partially.
it works on an already saved object, but not in an unsaved new object.
when i create a one2many line manually in the form ii appears immediately, and when i save the object the relation id is automatically written in the one2many object.

what i try to achieve is creating a new object and add some one2many line from code without the need of saving the object.
when i do it like you have written, the lines in one2many are written in database without the id of the parent object of the one2many. after saving the parent the id of the parent
is not in the one2many lines.

when i add some new one2many lines in an saved parent the parent id is available an can be written in the one2many line.

in odoo versions lower then 15 it was possible to add lines to a one2many field like
lines_ids = [(0,0, {'value': something})]
and the line was visible immediately.
hope it is understanable what i have written, my english is not the best :-)

Imagine profil
Klaus
Autor Cel mai bun răspuns

wow, thx so much for your answer. thats what i thought and found nothing in internet, or possible i searched not enough. :-) 

I was looking for a resolution for days now. in the odoo versions lower then 15 it always worked.

thx a lot i will try this tomorrow 


0
Imagine profil
Abandonează
Enjoying the discussion? Don't just read, join in!

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

Înscrie-te
Related Posts Răspunsuri Vizualizări Activitate
one2many fields, advise/guidance needed... Rezolvat
one2many odoo16features
Imagine profil
Imagine profil
1
sept. 23
2557
Filter One2many field in res.partner Rezolvat
filter one2many odoo16features
Imagine profil
Imagine profil
1
ian. 24
2561
Many2one not filled until One2many is saved Odoo 16
many2one one2many odoo16features
Imagine profil
Imagine profil
1
dec. 22
3301
Pull and Display a One2Many Field from a relation through a One2Many Field Rezolvat
one2many odoostudio odoo16features odoo-online
Imagine profil
1
iun. 23
4443
how to display one2many field on form view, as list of form views, not tree view.
one2many display formview odoo16features
Imagine profil
Imagine profil
Imagine profil
2
iun. 23
8606
Comunitate
  • Tutorials
  • Documentație
  • Forum
Open Source
  • Descărcare
  • Github
  • Runbot
  • Translations
Servicii
  • Hosting Odoo.sh
  • Suport
  • Actualizare
  • Custom Developments
  • Educație
  • Găsiți un contabil
  • Găsește un Partener
  • Devino Partener
Despre Noi
  • Compania noastră
  • Active de marcă
  • Contactați-ne
  • Locuri de muncă
  • Evenimente
  • Podcast
  • Blog
  • Clienți
  • Aspecte juridice • Confidențialitate
  • Securitate
الْعَرَبيّة 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 este o suită de aplicații de afaceri open source care acoperă toate nevoile companiei dvs.: CRM, comerț electronic, contabilitate, inventar, punct de vânzare, management de proiect etc.

Propunerea de valoare unică a Odoo este să fie în același timp foarte ușor de utilizat și complet integrat.

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