製品の一意性の制限を追加したら、コピーボタンは使えません.

4646 ワード

api.co nstrinsを使用して製品のnameを制限しています.製品の名前を唯一にすることを目的としていますが、コピーを使用していると、エラーが発生しました.既存の製品名を削除したら、製品名は一つしかないと気づきました.コピーされた製品名にはコピーの表示がありますが、なぜこのようなエラーが報告されますか?
研究の結果、コピーボタンをクリックすると、二回のcontrinsのチェック方法が呼び出されます.一回は製品名(副本)を使って、二回目は製品名を使って、search方法で検索したら、結果はなんと2つです.
なぜ二回のチェック方法を呼び出しますか?その後、親のcopy方法で原因を見つけました.
def copy(self, cr, uid, id, default=None, context=None):

        """ copy(default=None)



        Duplicate record with given id updating it with default values



        :param dict default: dictionary of field values to override in the

               original values of the copied record, e.g: ``{'field_name': overridden_value, ...}``

        :returns: new record



        """

        if context is None:

            context = {}

        context = context.copy()

        data = self.copy_data(cr, uid, id, default, context)

        new_id = self.create(cr, uid, data, context)

        self.copy_translations(cr, uid, id, new_id, context)

        return new_id
Copy方法は新たなrecordを作成した後、copy_を呼び出しました.translationsの方法で、間違いがなければ、問題はここにあるはずです.
def copy_translations(self, cr, uid, old_id, new_id, context=None):

               ……………………

                for record in trans_obj.read(cr, uid, trans_ids, context=context):

                    del record['id']

                    # remove source to avoid triggering _set_src

                    del record['source']

                    record.update({'res_id': target_id})

                    if user_lang and user_lang == record['lang']:

                        # 'source' to force the call to _set_src

                        # 'value' needed if value is changed in copy(), want to see the new_value

                        record['source'] = old_record[field_name]

                        record['value'] = new_record[field_name]

                    trans_obj.create(cr, uid, record, context=context)
不思議なことに、この中にはconstrainsに関するコードが含まれていません.むしろrecordが疑われます.出力レコードは「'name':'product.template,name'」のような値が見られますが、なぜtransplationがcontraninsをトリガするのか分かりません.
エラーコードのヒントを追跡して、low一級の_を発見しました.create方法の中で
 
for field in upd_todo:

            result += self._columns[field].set(cr, self, id_new, field, vals[field], user, rel_context) or []
 
 writeメソッドをトリガーしましたが、write方法はまた_をトリガしました.validate_fieldsは2回の検証をもたらします.
トランスデューサのソースフィールドは、その理由から、copy_だけが必要です.translationsにoldを入れます.IDを新しいものに置き換えることで、問題を回避することができます.