2017-11-02 9 views
1

私はpurchase.orderを継承し、選択フィールド(固定、パーセンテージ)とフロートフィールドを追加しました。パーセンテージを選択すると、浮動小数点値は100を超えてはならず、エラーがスローされます。オードーの選択に基づいてUsererrorを上げる方法?

どうすればこの問題を解決できますか?

これは私のコード

class PurchaseOrder(models.Model): 
_inherit = "purchase.order" 
_description="Purchase the products" 

discount=fields.Selection([('fixed','fixed Price'),('percentage','Percentage')],string="Discount") 
amount=fields.Float("Amount") 

@api.multi 
@api.constrains('amount') 
def Limited(self): 
    if self.discount=='percentage'and self.amount > 100: 
     raise UserError(_('Please enter proper amount')) 
+1

あなたのコードでは、私には良いようです。現在の出力で質問を編集できますか? –

+0

@Odedraこれは1つの時間エラーを示します。私に他の方法を教えてください – Naveen

+1

_On change_メソッドで行くことができます。 –

答えて

0

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

@api.onchange('amount','discount') 
def onchange_amount_discount(self): 
    if self.discount=='percentage' and self.amount > 100: 
     raise UserError(_('Please enter proper amount') 

注:割引フィールドを更新するとき

ONCHANGEメソッドが起動されます。

+0

投票の理由は何ですか? –

1

あなたはこのコードを試すことができます。

@api.one 
@api.constrains('amount') 
def Limited(self): 
    if self.discount=='percentage' and self.amount > 100: 
     raise UserError(_('Please enter proper amount')) 
関連する問題