Skip to Content
Odoo Menu
  • Prijavi
  • Try it free
  • Aplikacije
    Finance
    • Knjigovodstvo
    • Obračun
    • Stroški
    • Spreadsheet (BI)
    • Dokumenti
    • Podpisovanje
    Prodaja
    • CRM
    • Prodaja
    • POS Shop
    • POS Restaurant
    • Naročnine
    • Najem
    Spletne strani
    • Website Builder
    • Spletna trgovina
    • Blog
    • Forum
    • Pogovor v živo
    • eUčenje
    Dobavna veriga
    • Zaloga
    • Proizvodnja
    • PLM
    • Nabava
    • Vzdrževanje
    • Kakovost
    Kadri
    • Kadri
    • Kadrovanje
    • Odsotnost
    • Ocenjevanja
    • Priporočila
    • Vozni park
    Marketing
    • Družbeno Trženje
    • Email Marketing
    • SMS Marketing
    • Dogodki
    • Avtomatizacija trženja
    • Ankete
    Storitve
    • Projekt
    • Časovnice
    • Storitve na terenu
    • Služba za pomoč
    • Načrtovanje
    • Termini
    Produktivnost
    • Razprave
    • Odobritve
    • IoT
    • Voip
    • Znanje
    • WhatsApp
    Third party apps Odoo Studio Odoo Cloud Platform
  • Industrije
    Trgovina na drobno
    • Book Store
    • Trgovina z oblačili
    • Trgovina s pohištvom
    • Grocery Store
    • Trgovina s strojno opremo računalnikov
    • Trgovina z igračami
    Food & Hospitality
    • Bar and Pub
    • Restavracija
    • Hitra hrana
    • Guest House
    • Beverage Distributor
    • Hotel
    Nepremičnine
    • Real Estate Agency
    • Arhitekturno podjetje
    • Gradbeništvo
    • Estate Management
    • Vrtnarjenje
    • Združenje lastnikov nepremičnin
    Svetovanje
    • Računovodsko podjetje
    • Odoo Partner
    • Marketinška agencija
    • Law firm
    • Pridobivanje talentov
    • Audit & Certification
    Proizvodnja
    • Tekstil
    • Metal
    • Pohištvo
    • Hrana
    • Brewery
    • Poslovna darila
    Health & Fitness
    • Športni klub
    • Trgovina z očali
    • Fitnes center
    • Wellness Practitioners
    • Lekarna
    • Frizerski salon
    Trades
    • Handyman
    • IT Hardware & Support
    • Sistemi sončne energije
    • Izdelovalec čevljev
    • Čistilne storitve
    • HVAC Services
    Ostali
    • Neprofitna organizacija
    • Agencija za okolje
    • Najem oglasnih panojev
    • Fotografija
    • Najem koles
    • Prodajalec programske opreme
    Browse all Industries
  • Skupnost
    Learn
    • Tutorials
    • Dokumentacija
    • Certifikati
    • Šolanje
    • Blog
    • Podcast
    Empower Education
    • Education Program
    • Scale Up! Business Game
    • Visit Odoo
    Get the Software
    • Prenesi
    • Compare Editions
    • Releases
    Collaborate
    • Github
    • Forum
    • Dogodki
    • Prevodi
    • Become a Partner
    • Services for Partners
    • Register your Accounting Firm
    Get Services
    • Find a Partner
    • Find an Accountant
    • Meet an advisor
    • Implementation Services
    • Sklici kupca
    • Podpora
    • Upgrades
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Get a demo
  • Določanje cen
  • Pomoč

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

  • CRM
  • e-Commerce
  • Knjigovodstvo
  • Zaloga
  • PoS
  • Projekt
  • MRP
All apps
You need to be registered to interact with the community.
All Posts People Badges
Ključne besede (View all)
odoo accounting v14 pos v15
About this forum
You need to be registered to interact with the community.
All Posts People Badges
Ključne besede (View all)
odoo accounting v14 pos v15
About this forum
Pomoč

Odoo 15 - compute method triggered twice every time?

Naroči se

Get notified when there's activity on this post

This question has been flagged
computecomputedcomputed-fieldsv15
2 Odgovori
7919 Prikazi
Avatar
Fred Nunes

Hi all,


I have noticed a behavior on some of my computed fields that I can't understand. I have the following field (toy example):

field_x = fields.Boolean(string="Field X", compute="_compute_field_x", store=True)


Then function  _compute_field_x is as follows:


@api.depends("field_y", "field_z") # none of these are computed def  _compute_field_x(self):
    for record in self:
        print('hi')
        if record.field_y = 'Something':
            record.field_x = "A"
        elif record.field_z = 'Something Else':
            record.field_x = "B"
        else:
            record.field_x = "C"
    

For some reason, whenever I change either field_y or field_z on the model's form view, the message "hi" is printed twice: once before the write() method is called (to update field_y or field_z), and then once again after the write method is concluded. And even stranger, is that when the method is called AFTER the write method, it record.field_y or record.field_z still show the values pre-write.

In my case, this is a problem, because I wanted to send an email warning when field_x changes, but if I do it on the compute method it will end up sending the email twice.

If someone could clarify this behavior for me, I'd really appreciate it!

1
Avatar
Opusti
Avatar
Fred Nunes
Avtor Best Answer

In case this might help someone in the future - I was able to "fix" the problem of sending the email twice by checking if we are working in the local cache. I noticed the first time the compute function ran, the record being modified had an ID of type "odoo.models.NewId", which I assume is a temporary object Odoo uses  while the form is open, before actually writing to the database. So I simply check this and prevent the email functionality from running:

if not isinstance(record.id, models.NewId):

    send_email(...)

else:

    pass


3
Avatar
Opusti
Avatar
Waleed Ali Mohsen
Best Answer

Hi, Regardless of the code you posted as the code has some syntax errors and will not work and I think you provided it as example.

The computed method  (if store = True) is called once one of the dependencies fields changes so it will be called once you change the value in the form and it will be invoked on a pseudo-record that contains the values present in the form and not yet saved to the DB (  pseudo-record ) so you will get the first message and once you save the record the write method called and the value of field_y or fieldz changes in DB so the computed  will be called again. 

BTW, if computed field is not stored the computed method will be called every time when the field is accessed.

To send your email I prefer to override write method and send your email whenever the field_x changes.


0
Avatar
Opusti
Fred Nunes
Avtor

Hi Waleed, thanks for your comment, it clarified the situation.
Overwriting the "write" method was my initial approach, as I am already doing it with other fields. However, it seems that the write method is not invoked when a computed field is changed.
To explain better: when I changed field_y (to "Something", for example), field_x is recomputed, but the write method is only called once with values = {"field_y": "Something"}. The field_x does not show up. I am not sure how Odoo is writing field_x to the database in the backend, but it does not go through Model.write!

Waleed Ali Mohsen

The computed method called every time if there is changes in the field and depend on your condition the field_x will be recomputed but the field_x will not shown in write method values. I'm not diving deep on how the computed field calculated and updated in DB but I think they update field_x using SQL Command. Will try to search and let you know

Fred Nunes
Avtor

Thanks Waleed :) if the write method was called with the updated values, it would indeed fix my problem. For now I haven't found a solution but it's not critical.
Thanks for your effort!

Waleed Ali Mohsen

The computed field is not shown in write method and it's save in cache and then set to the DB from cache. So you don't the calculated field you can check your condition directly from overrided write and create method.

Fred Nunes
Avtor

Sorry, I don't understand what you mean by "So you don't the calculated field you can check your condition directly from overrided write and create method."!
But thanks for clarifying the cache thing, makes sense!

Waleed Ali Mohsen

Sorry I mean if the field_x is only flag to send email, you don't want it just put the same condition you did in computed field in write method and send the email.

Fred Nunes
Avtor

That was a good suggestion, unfortunately field_x is not a flag but rather a value. I want to send an email updating of the new value. I can probably hack some way to infer if the value changed or not during the write method :) I'll have to see if the additional complexity is worth it.
Thanks a lot!

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

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

Prijavi
Related Posts Odgovori Prikazi Aktivnost
Compute field to store in DB
computed-fields v15
Avatar
Avatar
1
jun. 23
3931
computed field doesnt work with a domain
fleet computed-fields v15
Avatar
0
apr. 24
2298
Why this computed field doest not work? Please Need Help
computed computed-fields solution
Avatar
Avatar
1
maj 23
3102
Compute if time is Between 00:00 and 07:00. Solved
datetime compute v15
Avatar
1
jan. 23
2843
odoo 15 compute days differance Solved
attributeerror computed-fields v15
Avatar
Avatar
Avatar
2
sep. 22
3314
Community
  • Tutorials
  • Dokumentacija
  • Forum
Open Source
  • Prenesi
  • Github
  • Runbot
  • Prevodi
Services
  • Odoo.sh Hosting
  • Podpora
  • Nadgradnja
  • Custom Developments
  • Izobraževanje
  • Find an Accountant
  • Find a Partner
  • Become a Partner
About us
  • Our company
  • Sredstva blagovne znamke
  • Kontakt
  • Zaposlitve
  • Dogodki
  • Podcast
  • Blog
  • Stranke
  • Pravno • Zasebnost
  • Varnost
الْعَرَبيّة 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 a suite of open source business apps that cover all your company needs: CRM, eCommerce, accounting, inventory, point of sale, project management, etc.

Odoo's unique value proposition is to be at the same time very easy to use and fully integrated.

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