Overslaan naar inhoud
Odoo Menu
  • Aanmelden
  • Probeer het gratis
  • Apps
    Financiën
    • Boekhouding
    • Facturatie
    • Onkosten
    • Spreadsheet (BI)
    • Documenten
    • Ondertekenen
    Verkoop
    • CRM
    • Verkoop
    • Kassasysteem winkel
    • Kassasysteem Restaurant
    • Abonnementen
    • Verhuur
    Websites
    • Websitebouwer
    • E-commerce
    • Blog
    • Forum
    • Live Chat
    • eLearning
    Bevoorradingsketen
    • Voorraad
    • Productie
    • PLM
    • Inkoop
    • Onderhoud
    • Kwaliteit
    Personeelsbeheer
    • Werknemers
    • Werving & Selectie
    • Verlof
    • Evaluaties
    • Aanbevelingen
    • Wagenpark
    Marketing
    • Social media Marketing
    • E-mailmarketing
    • SMS Marketing
    • Evenementen
    • Marketingautomatisering
    • Enquêtes
    Diensten
    • Project
    • Urenstaten
    • Buitendienst
    • Helpdesk
    • Planning
    • Afspraken
    Productiviteit
    • Chat
    • Goedkeuringen
    • IoT
    • VoIP
    • Kennis
    • WhatsApp
    Apps van derden Odoo Studio Odoo Cloud Platform
  • Bedrijfstakken
    Detailhandel
    • Boekhandel
    • kledingwinkel
    • Meubelzaak
    • Supermarkt
    • Bouwmarkt
    • Speelgoedwinkel
    Food & Hospitality
    • Bar en Pub
    • Restaurant
    • Fastfood
    • Gastenverblijf
    • Drankenhandelaar
    • Hotel
    Vastgoed
    • Makelaarskantoor
    • Architectenbureau
    • Bouw
    • Vastgoedbeheer
    • Tuinieren
    • Vereniging van eigenaren
    Consulting
    • Accountantskantoor
    • Odoo Partner
    • Marketingbureau
    • Advocatenkantoor
    • Talentenwerving
    • Audit & Certificering
    Productie
    • Textiel
    • Metaal
    • Meubels
    • Eten
    • Brewery
    • Relatiegeschenken
    Gezondheid & Fitness
    • Sportclub
    • Opticien
    • Fitnesscentrum
    • Wellness-medewerkers
    • Apotheek
    • Kapper
    Trades
    • Klusjesman
    • IT-hardware & support
    • Zonne-energiesystemen
    • Schoenmaker
    • Schoonmaakdiensten
    • HVAC-diensten
    Andere
    • Non-profitorganisatie
    • Milieuagentschap
    • Verhuur van Billboards
    • Fotograaf
    • Fietsleasing
    • Softwareverkoper
    Browse all Industries
  • Community
    Leren
    • Tutorials
    • Documentatie
    • Certificeringen
    • Training
    • Blog
    • Podcast
    Versterk het onderwijs
    • Onderwijs- programma
    • Scale Up! Business Game
    • Bezoek Odoo
    Download de Software
    • Downloaden
    • Vergelijk edities
    • Releases
    Werk samen
    • Github
    • Forum
    • Evenementen
    • Vertalingen
    • Word een Partner
    • Services for Partners
    • Registreer je accountantskantoor
    Diensten
    • Vind een partner
    • Vind een boekhouder
    • Een adviseur ontmoeten
    • Implementatiediensten
    • Klantreferenties
    • Ondersteuning
    • Upgrades
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Vraag een demo aan
  • Prijzen
  • Help

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

  • CRM
  • e-Commerce
  • Boekhouding
  • Voorraad
  • PoS
  • Project
  • MRP
All apps
Je moet geregistreerd zijn om te kunnen communiceren met de community.
Alle posts Personen Badges
Labels (Bekijk alle)
odoo accounting v14 pos v15
Over dit forum
Je moet geregistreerd zijn om te kunnen communiceren met de community.
Alle posts Personen Badges
Labels (Bekijk alle)
odoo accounting v14 pos v15
Over dit forum
Help

[odoo 8 ] : How to update value of one2many field with at second level by using create and write method using API?

Inschrijven

Ontvang een bericht wanneer er activiteit is op deze post

Deze vraag is gerapporteerd
one2manycreateupdatewriteodooV8
6 Antwoorden
64807 Weergaven
Avatar
Prince Caspion

Hello All,

I've asked this Question again, because i was not able to EDIT my question.

I'm having problem on overriding create method.

I've one2many field in my current model, I want to update the value of that field when new record created, and this one2many field's co-model has it's own one2many fields.

So, basically there is two levels of one2many fields.

My one2many field has one2many field, I want to create record for that model as well,

How can i achieve this?

Any help on this appreciated.

This is need to be done using api coding standard.

EDIT

E.g.

Class A(models.Model):    
_name = 'main.model'
x_ids = fields.one2Many('sub.model','main_id',"Test")

   

Class B(models.Model)    
_name = 'sub.model'   
main_id = fields.Many2one('main.model', "Main Model")
#remaining fields.
xx_ids = fields.One2many('another.sub.model','sub_model_id',"Test")
Class C(models.Model)    
_name = 'another.submodel'
sub_model_id = fields.Many2one('sub.model',"Sub Model")
#remaining fields..go here   

Consider this is model structure, now when i create record of Class A model, I want to updated one2many field of Class B and Class B is having Class C one2many field, so how to update the values or create records with 2 level of one2many field? is there any option?

I am looking for the solution for both side create and write method. Please anyone has idea than it will be really helpful.

Regards,

Prince

2
Avatar
Annuleer
Avatar
Deepa Venkatesh
Beste antwoord

You can achieve that in 2 ways:

Case 1: Default Values

if you have fixed data, then you can set default values in both objects i.e in ClassA set default values for 'x_ids' and in ClassB set default values for 'xx_ids'

Case 2: Create/Write method

In the First object (ClassA): while creating itself you can add its lines using One2Many and/or Many2Many semantics.


According to your sample object hierarchy:


@api.model

def create(self, vals):

valsC = {....} # dict of values needed for creating ClassC

valsB = {
'xx_ids': [(0, 0, valsC)],

          ....} # dict of values needed for creating ClassB

vals['x_ids'] = [(0, 0, valsB)] # appending it into the values of ClassA

return super(A, self).create(vals)



In Odoo, at the time of creating a record, you can pass values to its child be it a One2Many or Many2Many by using its semantics, here [(0,0,{})] represents am trying to create new One2Many record.

Note:

In valsB, I have appended the values of ClassC, and this dict am appending the original Vals of Class A, which will be like passing nested dict of values into ClassA

Similarly there exists a semantics for editing/updating, deleting and so, please refer the below samples which are used for both One2Many & Many2Many.


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

7
Avatar
Annuleer
Avatar
Kirubanidhi Rajarathinam
Beste antwoord

Update  A SAMPLE one2many record in odoo9, 

search_var = self.browse(self.id)

search_var.update({

'stud_ids': [(0,_, {

'reg_no':4200,

'dob': '04/01/1992',

'stud_name':'amuthan',

})]

})

1
Avatar
Annuleer
Avatar
Qutechs, Ahmed M.Elmubarak
Beste antwoord

Hello,

In creation you need to override the create method of class A to be something as:

@api.model

def create(self, vals):

res = super(A, self).create(vals)

#creating C record

c_res = self.env['another.submodel'].create({'field_name': value, ...})

#creating B record

b_res = self.env['sub.model'].create({'field_name': value, ...})

#Adding C to B

b_res.xx_ids += c_res

#Adding B to A [it is also a One2many]

res.x_ids += b_res

return res

You can do similar thing in the write method with some modifications ...


Hope this could helps ...

0
Avatar
Annuleer
Prince Caspion
Auteur

@Ahmed: Thanks for the nice example, i will try and check it.

Prince Caspion
Auteur

Ahmed given solution does not work , gives me this error, defaults.update(values)

ValueError: dictionary update sequence element #0 has length 3; 2 is required

Qutechs, Ahmed M.Elmubarak

Hi, can you share your code some where ?

Geniet je van het gesprek? Blijf niet alleen lezen, doe ook mee!

Maak vandaag nog een account aan om te profiteren van exclusieve functies en deel uit te maken van onze geweldige community!

Aanmelden
Gerelateerde posts Antwoorden Weergaven Activiteit
Best way to create one2many records in backend
one2many create update
Avatar
0
nov. 22
4969
how to override create or write method Opgelost
one2many create write
Avatar
Avatar
Avatar
3
jul. 17
12681
update one2many field
function one2many create update
Avatar
0
apr. 19
4295
[odoo 8]: How set value of one2many field by overriding create method with api coding standard? Opgelost
one2many create api override odooV8
Avatar
Avatar
2
dec. 20
9201
how to update odoo One2many field from list of dictionaries?
one2many create update loop dictionary
Avatar
Avatar
3
jul. 20
18768
Community
  • Tutorials
  • Documentatie
  • Forum
Open Source
  • Downloaden
  • Github
  • Runbot
  • Vertalingen
Diensten
  • Odoo.sh Hosting
  • Ondersteuning
  • Upgrade
  • Gepersonaliseerde ontwikkelingen
  • Onderwijs
  • Vind een boekhouder
  • Vind een partner
  • Word een Partner
Over ons
  • Ons bedrijf
  • Merkelementen
  • Neem contact met ons op
  • Vacatures
  • Evenementen
  • Podcast
  • Blog
  • Klanten
  • Juridisch • Privacy
  • Beveiliging
الْعَرَبيّة 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 is een suite van open source zakelijke apps die aan al je bedrijfsbehoeften voldoet: CRM, E-commerce, boekhouding, inventaris, kassasysteem, projectbeheer, enz.

Odoo's unieke waardepropositie is om tegelijkertijd zeer gebruiksvriendelijk en volledig geïntegreerd te zijn.

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