Skip to Content
Odoo Menu
  • Log ind
  • Prøv gratis
  • Apps
    Økonomi
    • Bogføring
    • Fakturering
    • Udgifter
    • Regneark (BI)
    • Dokumenter
    • e-Signatur
    Salg
    • CRM
    • Salg
    • POS Butik
    • POS Restaurant
    • Abonnementer
    • Udlejning
    Hjemmeside
    • Hjemmesidebygger
    • e-Handel
    • Blog
    • Forum
    • LiveChat
    • e-Læring
    Forsyningskæde
    • Lagerbeholdning
    • Produktion
    • PLM
    • Indkøb
    • Vedligeholdelse
    • Kvalitet
    HR
    • Medarbejdere
    • Rekruttering
    • Fravær
    • Medarbejdersamtaler
    • Anbefalinger
    • Flåde
    Marketing
    • Markedsføring på sociale medier
    • E-mailmarketing
    • SMS-marketing
    • Arrangementer
    • Automatiseret marketing
    • Spørgeundersøgelser
    Tjenester
    • Projekt
    • Timesedler
    • Udkørende Service
    • Kundeservice
    • Planlægning
    • Aftaler
    Produktivitet
    • Dialog
    • Godkendelser
    • IoT
    • VoIP
    • Vidensdeling
    • WhatsApp
    Tredjepartsapps Odoo Studio Odoo Cloud-platform
  • Brancher
    Detailhandel
    • Boghandel
    • Tøjforretning
    • Møbelforretning
    • Dagligvarebutik
    • Byggemarked
    • Legetøjsforretning
    Mad og værtsskab
    • Bar og pub
    • Restaurant
    • Fastfood
    • Gæstehus
    • Drikkevareforhandler
    • Hotel
    Ejendom
    • Ejendomsmægler
    • Arkitektfirma
    • Byggeri
    • Ejendomsadministration
    • Havearbejde
    • Boligejerforening
    Rådgivning
    • Regnskabsfirma
    • Odoo-partner
    • Marketingbureau
    • Advokatfirma
    • Rekruttering
    • Audit & certificering
    Produktion
    • Tekstil
    • Metal
    • Møbler
    • Fødevareproduktion
    • Bryggeri
    • Firmagave
    Heldbred & Fitness
    • Sportsklub
    • Optiker
    • Fitnesscenter
    • Kosmetolog
    • Apotek
    • Frisør
    Håndværk
    • Handyman
    • IT-hardware og support
    • Solenergisystemer
    • Skomager
    • Rengøringsservicer
    • VVS- og ventilationsservice
    Andet
    • Nonprofitorganisation
    • Miljøagentur
    • Udlejning af billboards
    • Fotografi
    • Cykeludlejning
    • Softwareforhandler
    Gennemse alle brancher
  • Community
    Få mere at vide
    • Tutorials
    • Dokumentation
    • Certificeringer
    • Oplæring
    • Blog
    • Podcast
    Bliv klogere
    • Udannelselsesprogram
    • Scale Up!-virksomhedsspillet
    • Besøg Odoo
    Få softwaren
    • Download
    • Sammenlign versioner
    • Udgaver
    Samarbejde
    • Github
    • Forum
    • Arrangementer
    • Oversættelser
    • Bliv partner
    • Tjenester til partnere
    • Registrér dit regnskabsfirma
    Modtag tjenester
    • Find en partner
    • Find en bogholder
    • Kontakt en rådgiver
    • Implementeringstjenester
    • Kundereferencer
    • Support
    • Opgraderinger
    Github Youtube Twitter LinkedIn Instagram Facebook Spotify
    +1 (650) 691-3277
    Få en demo
  • Prissætning
  • Hjælp

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

  • CRM
  • e-Commerce
  • Bogføring
  • Lager
  • PoS
  • Projekt
  • MRP
All apps
Du skal være registreret for at interagere med fællesskabet.
All Posts People Emblemer
Tags (View all)
odoo accounting v14 pos v15
Om dette forum
Du skal være registreret for at interagere med fællesskabet.
All Posts People Emblemer
Tags (View all)
odoo accounting v14 pos v15
Om dette forum
Hjælp

odoo 16 One2many insert value doesnt work

Tilmeld

Få besked, når der er aktivitet på dette indlæg

Dette spørgsmål er blevet anmeldt
one2manyvalueaddodoo16features
2 Besvarelser
3531 Visninger
Avatar
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
Avatar
Kassér
Avatar
Sayed Mohammed Aqeel Ebrahim
Bedste svar

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
Avatar
Kassér
Klaus
Forfatter

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
Forfatter

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 :-)

Avatar
Klaus
Forfatter Bedste svar

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
Avatar
Kassér
Enjoying the discussion? Don't just read, join in!

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

Tilmeld dig
Related Posts Besvarelser Visninger Aktivitet
one2many fields, advise/guidance needed... Løst
one2many odoo16features
Avatar
Avatar
1
sep. 23
2538
Filter One2many field in res.partner Løst
filter one2many odoo16features
Avatar
Avatar
1
jan. 24
2542
Many2one not filled until One2many is saved Odoo 16
many2one one2many odoo16features
Avatar
Avatar
1
dec. 22
3297
Pull and Display a One2Many Field from a relation through a One2Many Field Løst
one2many odoostudio odoo16features odoo-online
Avatar
1
jun. 23
4425
how to display one2many field on form view, as list of form views, not tree view.
one2many display formview odoo16features
Avatar
Avatar
Avatar
2
jun. 23
8577
Community
  • Tutorials
  • Dokumentation
  • Forum
Open Source
  • Download
  • Github
  • Runbot
  • Oversættelser
Tjenester
  • Odoo.sh-hosting
  • Support
  • Opgradere
  • Individuelt tilpasset udvikling
  • Uddannelse
  • Find en bogholder
  • Find en partner
  • Bliv partner
Om os
  • Vores virksomhed
  • Brandaktiver
  • Kontakt os
  • Stillinger
  • Arrangementer
  • Podcast
  • Blog
  • Kunder
  • Juridiske dokumenter • Privatlivspolitik
  • Sikkerhedspolitik
الْعَرَبيّة 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 er en samling open source-forretningsapps, der dækker alle dine virksomhedsbehov – lige fra CRM, e-handel og bogføring til lagerstyring, POS, projektledelse og meget mere.

Det unikke ved Odoo er, at systemet både er brugervenligt og fuldt integreret.

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