コンテンツへスキップ
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
このフォーラムについて
ヘルプ

How to override onchange old api vs new ?

購読

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

この質問にフラグが付けられました
v7apionchangeoverridev8.0
7 返信
15860 ビュー
アバター
Temur

Need to override onchange function from old v7 API, like this one:

Python (v7 API):

...
def onchange_field_a(self, cr, uid, ids, field_a, field_b, field_c, context=None):
    vals = {'value':{} }
    #
    # A huge code here to be executed on field_a changes...
    #
    return vals
...

XML (v7 API):

...
    <field name="field_a" on_change="onchange_field_a(field_a, field_b, field_c)"
...

v7 onchange function said to be incompatible with the new v8 api. here is related posts on this forum that I've found for now:

https://www.odoo.com/forum/help-1/question/75299 

https://www.odoo.com/forum/help-1/question/73893 

- however, we'd like to override onchange_field_a function using new v8.0 API and 1) we do not want to rewrite a huge code written in v7 onchange function, which is well tested and working in the parent model and 2) we do not want to copy-paste it either. Beside that it's bad practice to have copy of a same code in several places, it should be harder to maintain such a copy, as it's likely to receive further bugfixes and improvements (if any) in the same v7 implementation and the same place where it's defined. So, we need to reuse existing v7 onchange_field_a function code defined in the superclass by calling it from overridden v8 onchange_field_a function and add our additional code afterwards (or before,depending on case). 

1
アバター
破棄
アバター
Temur
著作者 最善の回答

right question to ask here is what it makes incompatible onchange v7 vs v8? and can we overcome this incompatibility in order to reuse existing code? Actually it's possible by applying quite simple workaround to the onchange v7 vs v8 incompatibility:

Python (v8 API):

...
@api.onchange("field_a")
@api.depends("field_b", "field_c", "custom_field_in_my_class")
def onchange_field_a(self):

### section 1. get result of original onchange_field_a from superclass:
orig_result = super(my_class,self).onchange_field_a(self.field_a, self.field_b, self.field_c)

### section 2. workaround for api incompatibility:
if type(orig_result) is dict and orig_result.has_key('value'):
for field, value in orig_result.get('value').items():
if hasattr(self, field):
setattr(self, field, value)


### section 3. your additional code, for example:
if self.field_c:
self.custom_field_in_my_class = True
...

Old api way was to return value dictionary for changed fields, whereas new api way is to assign changes directly to fields of virtual record inside the onchange function, so in the "section 2" we turn value return through dictionary into direct assignments to fields. that's it, api incompatibility is handled by the "section 2".

Note:
field's "on_change" property in XML is no more necessary in v8 

2
アバター
破棄
Temur
著作者
XML part: we can just ignore XML part in this case. it'll work with or without removing on_change property from corresponding XML record, this record just become redundant, but do not interfere normal execution of this overridden function.
Temur
著作者
Note for Relational Fields: if any field from @api.onchange or @api.depends lists are relational fields, then in "section1" you have to use self.field_name.id or self.field_name.ids respectively for many2one and x2many fields. for example, if field_a has a type Many2one and field_b has a type one2many or many2many, then instead of:
orig_result = super(my_class,self).onchange_field_a(self.field_a, self.field_b, self.field_c)
we'll have:
orig_result = super(my_class,self).onchange_field_a(self.field_a.id, self.field_b.ids, self.field_c)
Temur
著作者
Return Value: as it's v8.0 style onchange function (@api.onchange), we do NOT need to return anything. the "orig_result" result of call parent function is just used in the "section 2", where it's turned into direct assignments. No need to return anything, "section 3" is just normal v8.0 @api.onchange code with direct assignments to fields of "self"
Gavin Yap

Hi Temur, I've tried to over the onchange methood fo hr.payslip

 
    @api.onchange("contract_id")
    @api.depends( "date_from", "date_to", "employee_id", "contract_id")
    def onchange_field_a(self):
        print "do something"
it doesn't work for contract_id. Can you advise?
Temur
著作者

if there is already a XML entry with on_change in a corresponding form view, then it may require to be replaced with on_change="1", Try to add attribute directly: <field name="contract_id" position="attributes"> <atribute name="on_change" >1</attribute></field>, if it does not work then you can simply replace entire contract_id field with itself, but with on_change="1" (pay attention to other properties in field definition as well, if there is any, in order to preserve them by the way)

Temur
著作者

Here is original definition of contract_id in it's view:

<field name="contract_id" domain="[('employee_id','=',employee_id),('date_start','&lt;=',date_to),'|',('date_end','&gt;=',date_from),('date_end','=',False)]" on_change="onchange_contract_id(date_from, date_to, employee_id, contract_id)" context="{'default_employee_id': employee_id}"/>
There is "domain" and "context" attributes as well, most probably you'll need to preserve them, but change the existing "on_change" property, in order to have the on_change="1" record in it's place. There is a few ways to do so. Here is one of the options, inherit the view and replace on_change property of contract_id (Note: please try the "attribute" way described in the previous comment first, it's interesting if that one will work), by replacing an entire field with itself, like:
<field name="contract_id" position="replace" >
<field name="contract_id" domain="[('employee_id','=',employee_id),('date_start','&lt;=',date_to),'|',('date_end','&gt;=',date_from),('date_end','=',False)]" on_change="1" context="{'default_employee_id': employee_id}" />
</field>
ディスカッションを楽しんでいますか?読むだけでなく、参加しましょう!

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

登録
関連投稿 返信 ビュー 活動
Override Onchange Method : onchange_partner_id on account_voucher [v7]
v7 method onchange override
アバター
アバター
アバター
5
4月 17
8293
How to override the onchange_product_tmpl_id with new api?
api inherit onchange override
アバター
アバター
アバター
アバター
3
11月 15
7964
API 8.0 onchange() and depends() decorators only work for UI changes and 1 record only?
api onchange
アバター
アバター
1
6月 15
5909
Is there a v7 API guide? 解決済
v7 api
アバター
アバター
1
3月 15
4934
Preventing On change recursion
v7 onchange
アバター
1
3月 15
6250
コミュニティ
  • チュートリアル
  • ドキュメンテーション
  • フォーラム
オープンソース
  • ダウンロード
  • 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