2016-08-25 1 views
2

私はどのように関数内の関数を呼び出すには?Odoo9検証が

class product_pricelist_item(models.Model): 
    _inherit = 'product.pricelist.item' 

    myfield = fields.Boolean(string="CheckMark") 

product.pricelist.itemにブールフィールドを追加した今product.pricelist.item内に複数の行があります。

(バリデーション) 私は、ユーザーがTrue複数のフィールドを一度にTrueすることができmyfield一対一を行うことが許可されていないことを望みます。

product.pricelist.itemでこれを試してみましたが、これにカウンタを付けてmyfieldsという数字のTrueを渡してみました。

これは私にエラーを与えています。

global name '_get_counter' is not defined

def _get_counter(self): 
    for r in self: 
     p=[] 
     p= r.env['product.pricelist.item'].search_read([('myfield', '=', True)], ['myfield']) 
     counter = len(p) 
    return counter 

@api.constrains('myfield') 
def _check_myfield(self): 
    counter = _get_counter(self) 
    for r in self: 
     if counter > 1: 
      raise exceptions.ValidationError("Multiple myfield In a PriceList of a Product is not allowed.") 

今、2番目の質問は次のとおりです。 -

あなたが価格表項目を作成し、それがデータベース内のデータを反映しない価格表に保存する]をクリックすると。あなたがpricelistをクリックすると、それはデータを反映します...なぜこれがそうですか?

答えて

3

selfでは、現在のクラスのメソッドを呼び出すことができます。

次のコードで試してみてください。

_get_counterのループが結果に影響しないとすることができますので、検索は、レコードには依存しませんでした

counter = self._get_counter() 
+0

エラー: - '_get_counter()は正確に1つの引数(2与えられます)' – maharshi

+0

'counter = self._get_counter()'うまく動作します。 – maharshi

1

とコード

counter = _get_counter(self) 

を交換してください使用:

def _get_counter(self): 

    pricelist_obj = self.env['product.pricelist.item'] 
    counter = len(pricelist_obj.search_read([('myfield', '=', True)], ['myfield'])) 
    return counter 

@api.constrains('myfield') 
def _check_myfield(self): 

    counter = self._get_counter() 
    if counter > 1: 
     raise exceptions.ValidationError("Multiple myfield In a PriceList of a Product is not allowed.") 
関連する問題