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

Create new Many2One records in @api.onchange method (2nd try)

購読

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

この質問にフラグが付けられました
many2oneonchangenewapi
5 返信
20334 ビュー
アバター
Tzin

Hello,

I am new to Odoo and I struggle with the new Odoo API. I am trying to create new records on a Many2One field in an @api.onchange method. It is a custom module so I will simplify by providing a made up example. 

Say that I am building a new Order that has Lines. The lines are defined via a Many2One field called 'line':

class Order(models.Model):

    name="mymodule.order"

    line = fields.One2many('mymodule.line',
                                inverse_name="order_id",         
                                string="Lines")

    partner_id = fields.....

    .....

I want to generate new lines as soon as the user selects or changes the partner_id of the order. To do so, I have implemented an @api.onchance method that looks somewhat like this:

@api.onchange('partner_id')
 def onchange_equipment_type_id(self):
        """ Updates the order lines"""
        if self.line:
            self.line.unlink()
        for val in self.bogus_values: #some bogus list that supposedly tells me how many lines I need to create
            self.line.create({'name':val.name, 'order_id':self.id, ....})

The above approach does not work. First of all, as far as I understand CREATE will actually create records to the DB. I do not want that. I simply want to add some order lines as if I clicked few times on 'Add an item' in a table. The actual creation of the lines shall happen when the user clicks the Save button on the order. So what method shall I use? How can I 'add' records to the line field without writing to the DB at this point? 

The second problem that I face is that self.id is NewId. Now as per my understanding this is normal for New records. But I actually get self.id as NewID even for entries that have been saved and I open them in edit mode. Anyways, for the case when the order has not been saved yet, I still need to add lines. So my major issue is how to add lines to my Many2One field as part of my onchange method without triggering a DB write. 

 

Let me reiterate again - I want to create a Many2One not as part of the 'parent' object create method. I want to do this as an onchange event.  I want to generate the lines as soon as I change the partner. Imagine that for each partner there is a predefined set of lines, for example based on favorite products. So as soon as the one changes the partner id, a new list of favorite products will be retrieved (for the selected partner), the existing order lines must be deleted and new lines (for the new product set) shall be generated. All this shall happen prior to the user hitting the Save button on the order object. Actually the user can change partners several times priori to saving the order. 

I hope that my question makes some sense. 

Thank you in advance for your help.

2
アバター
破棄
アバター
Tzin
著作者 最善の回答

Hi, I solved this problem some time in the following manner:

@api.onchange('partner_id') 
def _onchange_equipment(self):
    lines =[]
     
    for val in self.bogus_values:
        line_item = {
                      'attr1': val.name,
                      'attr2': val.a1,
                      ...                             
                }
        lines += [line_item]
    self.update({'line': lines})

In my very humble oppinion this works better than the proposal of Stefano (at least for my scenario). After all, the user can change the partner many time prior to saving the order. The create will probably insert new records in the DB. We do not want this to happen each time the user changes the partner. Stefano, please correct me if I am wrong and thank you for your response.

1
アバター
破棄
アバター
Jairo Llopis
最善の回答

Do not use create(). Use new() instead. It's also more object-oriented than update().

In your case, replace self.line.create({'name':val.name, 'order_id':self.id, ....}) with self.line |= self.line.new({'name':val.name ....})

5
アバター
破棄
Jairo Llopis

It should be documented IMHO. I opened bug https://github.com/odoo/odoo/issues/5611

Andre

Did it for me! Thanks.

sharon shen

new doesn't work for me, only create works... im on odoo 8

アバター
Yogesh
最善の回答

Try this, it will work

@api.onchange('partner_id')
 def onchange_equipment_type_id(self):
        """ Updates the order lines""
        for val in self.bogus_values: #some bogus list that supposedly tells me how many lines I need to create
            self.line  = [((0, 0, {'name':val.name, 'order_id':self.id, ....}))]

 

2
アバター
破棄
アバター
Raghupathy.k
最善の回答

Hai Tzin

I have solved it by the following code in odoo 15

@api.onchange('customer_id')
def _onchange_customer_id(self):
    lines =[]
    for rec in self.customer_id.site_ids:
        result ={'name':rec.name}
        lines.append((0,0, result))
    self.site_ids = lines

0
アバター
破棄
アバター
Stefano Del Gobbo
最善の回答

I need something similar.

I have to fill BoM list on selecting product.

@api.onchange('product_id')
 def onchange_product_id(self):
        """ Updates the BOM lines"""
        if self.line:
        for val in self.bom_template: #value on a BOM template that cointain products
            self.line.create({'name':val.name, 'order_id':self.id, ....})

 

Any help?

Thanks

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

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

登録
関連投稿 返信 ビュー 活動
when i select the category in the sale list the regarding product must shown in the product id in the bill list by onchange
many2one onchange
アバター
0
12月 18
13
Onchange many2one partner_bank_id 解決済
many2one onchange
アバター
アバター
1
5月 16
5029
Create dynamic selection on many2one fields with onchange
many2one onchange dynamic
アバター
アバター
1
9月 23
7322
Create new Many2One records in @api.onchange method
many2one create onchange
アバター
アバター
1
8月 22
8811
How to Use Many2one Field as selection for details?
many2one selection onchange
アバター
アバター
1
7月 19
7785
コミュニティ
  • チュートリアル
  • ドキュメンテーション
  • フォーラム
オープンソース
  • ダウンロード
  • 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