コンテンツへスキップ
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

購読

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

この質問にフラグが付けられました
many2onecreateonchange
1 返信
8796 ビュー
アバター
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((0, 0, {'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. 

I hope that my question makes some sense. 

Thank you in advance for your help.

1
アバター
破棄
Baiju KS

Hi Tzin, can you please explain your need to do this?? to know more about the question

Tzin
著作者

Thanks Baiiju, see my last comment for more details.

Baiju KS

Hi Ivan, Thank you very much for your response. Unfortunately, this is not what I am looking for. Initially I had it as self.line.create({'name':val.name, 'order_id':self.id, ....}). It did not work either - the self.id was NewID regardless if the Order was newly created or if it was an existing order that I was editing. I think there is something funny happening in @api.onchange. Now, the second proposal will not for me either. 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. This should also answer the clarification request from Baiiju. I believe that the best place is an onchange method but I am struggling in creating new Many2One-s inside the onchange handler. Reading Many2One is not a problem. And last but not least, I do not think I shall use create as it writes to the DB.

アバター
Ivan
最善の回答

You are mixing the syntax for 2many with create.  If you want to create the line record, don't use (0, 0, ...) the dictionary itself would do. So self.line.create({'name':val.name, 'order_id':self.id, ....}).  However this will presume that the order has been created.

If you want to create it together with the mymodule.order, then you use that syntax:
self.mymodule_order.create({.... 'line': [(0, 0, {'name':val.name, ....})]}) and you don't have to specify the 'order_id' because it will be added automatically.  Also you need to put the (0, 0, ...) tuple in a list.

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
5022
Create dynamic selection on many2one fields with onchange
many2one onchange dynamic
アバター
アバター
1
9月 23
7305
Create new Many2One records in @api.onchange method (2nd try) 解決済
many2one onchange newapi
アバター
アバター
アバター
アバター
アバター
5
3月 22
20323
Set Domain during Create method 解決済
domain create onchange
アバター
1
7月 20
4595
コミュニティ
  • チュートリアル
  • ドキュメンテーション
  • フォーラム
オープンソース
  • ダウンロード
  • 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