2016-11-26 11 views
2

私はOdoo 9の計算フィールドで検索しようとしていますが、結果としてすべてのレコードが返されます。どのようにしてOdooの計算フィールドで検索できますか?

class Version(models.Model): 
    _name='product_cars_application.version' 

    name = fields.Char(compute="_get_name", store=True) 

    @api.one 
    def _get_name(self): 
     self.name = "%s %s %s (%s)" % (self.brand_id, 
             self.model_id.name, 
             self.vname, 
             self.year_id) 

私はstore=Trueとし、それなしで試してみましたが、名前フィールドがdatabseに格納されていません。私もデータベースから列 "名前"を削除しようとしましたが、モジュールを更新しましたが、計算を保存していません。計算されたフィールドは、フォームビューで正常に動作しますが、名前は格納されず、検索は機能しません。

フィールド名で検索するにはどうすればよいですか?

答えて

1

これをコードに追加します。

def _name_search(self,name, args=None, operator='ilike', limit=100): 
    if operator == 'like': 
     operator = 'ilike' 

    versions=self.search([('name', operator, name)], limit=limit) 
    return versions.name_get() 

name = fields.Char(compute=_get_name, store=True,search=_name_search) 

詳細はドキュメントに記載されています。

https://www.odoo.com/documentation/8.0/reference/orm.html

節 "計算フィールドは、" あなたのためです。

うまくいけばうまくいきます。お知らせ下さい。

+0

は、いくつかの変更を しなければならないデフ_name_search(自己、名前、引数=なし、演算子= 'iLikeの'、リミット= 100): かの演算子== 'のように': 演算子= 'iLikeの' バージョン= self.search([( 'name'、operator、name)]、limit = limit) リターンバージョン.name_get() –

+0

私はあなたのニーズに私のコードを変更しました。同じ問題を抱えている誰かが、私たちがここでやったことをよりよく理解することができます。 – Nope

+0

計算されたフィールドで 'store = True'を使用することは、場合によってはうまく動作しないため、使用しないことをお勧めします。 – ChesuCR

0

計算フィールドではstore=Trueを使用することは推奨されません。なぜなら、場合によってはうまく機能しないためです。だから、私はあなたがこのような何かを行うことができると思います:あなたは、常にすべてのレコードをループする必要があるため

class Version(models.Model): 
    _name = 'product_cars_application.version' 

    name = fields.Char(
     compute="_compute_name", 
     search="_search_name", 
    ) 

    @api.one 
    def _compute_name(self): 
     self.name = "%s %s %s (%s)" % (self.brand_id, 
            self.model_id.name, 
            self.vname, 
            self.year_id) 

    def _search_name(self, operator, value): 
     """ Actually this converts a domain into another one. 
      With this new domain Odoo can search well 
      A list of ids is the most easy way without computed fields 
       * [('id', 'in', id_list)] 
     """ 
     if operator == 'ilike': 
      name = self.env.context.get('name', False) 
      if name is not False: 
       id_list = [] 
       product_cars = self.env['product_cars_application.version'].search([]) 
       for car in product_cars: 
        if name in car.name: 
         id_list.append(lot.id) 
       return [('id', 'in', lot_id_list)] 
      else: 
       return [('id', 'in', [])] 
     else: 
      _logger.error(
       'The field name is not searchable' 
       ' with the operator: {}',format(operator) 
      ) 

それは非効率的ですが、私はそれを行うための唯一のセーフモードだと思います。

ところで、具体的なケースでは、フィールド名を通常の文字として作成することができます。これは計算されません。デフォルト値を設定して名前を作成することができます。このように値は保存され、問題は消えてしまいます。

関連する問題