コンテンツへスキップ
Odoo メニュー
  • サインイン
  • 無料で15日試す
  • アプリ
    財務
    • 会計
    • 請求
    • 経費
    • スプレッドシート(BI)
    • ドキュメント管理
    • 署名
    販売
    • CRM
    • 販売
    • POS店舗
    • POSレストラン
    • サブスクリプション
    • レンタル
    ウェブサイト
    • ウェブサイトビルダー
    • eコマース
    • ブログ
    • フォーラム
    • ライブチャット
    • eラーニング
    サプライチェーン
    • 在庫
    • 製造
    • 製品ライフサイクル管理 (PLM)
    • 購買
    • 整備
    • 品質
    人事業務
    • 従業員管理
    • 採用
    • 休暇管理
    • 人事評価
    • リファラル
    • フリート
    マーケティング
    • ソーシャルマーケティング
    • メールマーケティング
    • SMSマーケティング
    • イベント
    • マーケティングオートメーション
    • アンケート調査
    サービス
    • プロジェクト管理
    • タイムシート
    • フィールドサービス
    • ヘルプデスク
    • 計画
    • アポイントメント
    生産性向上ツール
    • ディスカッション
    • 承認
    • IoT
    • VoIP
    • ナレッジ
    • WhatsApp
    サードパーティアプリ Odooスタジオ Odooクラウドプラットホーム
  • インダストリー(業種別ソリューション)
    小売
    • 書店
    • アパレルショップ
    • 家具専門店
    • 食料品店
    • 金物店
    • 玩具店
    飲食・ホスピタリティ業界
    • バー・パブ
    • レストラン
    • ファストフード
    • ゲストハウス
    • 飲料販売代理店
    • ホテル
    不動産
    • 不動産会社
    • 建築事務所
    • 建設
    • 不動産管理
    • 造園
    • 住宅所有者組合
    コンサルティング
    • 会計事務所
    • Odooパートナー
    • マーケティングエージェンシー
    • 法律事務所
    • 人材派遣
    • 監査・認証
    製造
    • テキスタイル
    • 金属
    • 家具
    • 飲食
    • 醸造所
    • コーポレートギフト
    ヘルス & フィットネス
    • スポーツクラブ
    • 眼鏡店
    • フィットネスセンター
    • ウェルネス専門家
    • 薬局
    • ヘアサロン
    業種
    • 便利屋
    • IT ハードウェア・サポート
    • 太陽エネルギーシステム
    • 靴メーカー
    • クリーニングサービス
    • 空調設備サービス
    その他
    • 非営利団体
    • 環境機関
    • ビルボードレンタル
    • 写真
    • 自転車リース
    • ソフトウェアリセラー
    すべての業種を見る
  • コミュニティ
    学ぶ
    • チュートリアル
    • ドキュメンテーション
    • 認定
    • トレーニング
    • ブログ
    • ポッドキャスト
    教育サポート
    • 教育プログラム
    • Scale Up! ビジネスゲーム
    • Odooオフィス訪問
    ソフトを入手
    • ダウンロード
    • エディションを比較
    • リリース
    コラボレーション
    • Github
    • フォーラム
    • イベント
    • 翻訳
    • パートナーになる
    • パートナー様向けサービス
    • 会計事務所を登録
    サービス利用
    • パートナー一覧
    • 会計事務所一覧
    • 今すぐ相談する
    • 導入支援サービス
    • お客様一覧
    • サポート
    • アップグレード
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    製品デモを利用する
  • 料金
  • ヘルプ

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

  • CRM
  • e-Commerce
  • 会計
  • 在庫
  • PoS
  • プロジェクト
  • MRP
All apps
コミュニティで交流するには登録する必要があります。
全てのポスト 人々 バッジ
タグ (全て表示)
odoo accounting v14 pos v15
このフォーラムについて
コミュニティで交流するには登録する必要があります。
全てのポスト 人々 バッジ
タグ (全て表示)
odoo accounting v14 pos v15
このフォーラムについて
ヘルプ

Odoo 15 - compute method triggered twice every time?

購読

この投稿に活動があった際に通知を受け取ります

この質問にフラグが付けられました
computecomputedcomputed-fieldsv15
2 返信
7823 ビュー
アバター
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
アバター
破棄
アバター
Fred Nunes
著作者 最善の回答

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
アバター
破棄
アバター
Waleed Ali Mohsen
最善の回答

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
アバター
破棄
Fred Nunes
著作者

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
著作者

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
著作者

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
著作者

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!

ディスカッションを楽しんでいますか?読むだけでなく、参加しましょう!

今すぐアカウントを作成して、限定機能を利用したり、素晴らしいコミュニティと交流しましょう!

登録
関連投稿 返信 ビュー 活動
Compute field to store in DB
computed-fields v15
アバター
アバター
1
6月 23
3838
computed field doesnt work with a domain
fleet computed-fields v15
アバター
0
4月 24
2223
Why this computed field doest not work? Please Need Help
computed computed-fields solution
アバター
アバター
1
5月 23
3030
Compute if time is Between 00:00 and 07:00. 解決済
datetime compute v15
アバター
1
1月 23
2770
odoo 15 compute days differance 解決済
attributeerror computed-fields v15
アバター
アバター
アバター
2
9月 22
3242
コミュニティ
  • チュートリアル
  • ドキュメンテーション
  • フォーラム
オープンソース
  • ダウンロード
  • Github
  • Runbot
  • 翻訳
サービス
  • Odoo.shホスティング
  • サポート
  • アップグレード
  • カスタム開発
  • 教育プログラム
  • 会計事務所一覧
  • パートナー一覧
  • パートナーになる
企業情報
  • 会社概要
  • ブランドアセット
  • お問い合わせ
  • 採用情報
  • イベント
  • ポッドキャスト
  • ブログ
  • お客様一覧
  • リーガル情報 • プライバシーポリシー
  • セキュリティ
الْعَرَبيّة 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(オドゥー)は、CRM、eコマース、会計、在庫管理、POS、プロジェクト管理など、企業のさまざまな業務を一つのシステムで管理できる、ベルギー発のオープンソースのERPソフトウェアです。

高機能で使いやすく、完全に統合されたERPとして、ユニークな価値を提供しています。

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