odoo 8:未更新计算字段的值

odoo 8: Value of Computed Field not updated

class aa_pettycash(models.Model):
    _name = "aa.pettycash"

    amount = fields.Float('Paid Amount', store=True)
    balance = fields.Float(compute=_get_balance, string='Balance', readonly=True, store=True)

    @api.multi
    @api.depends('journal_id')
    def _get_balance(self):
        aa_obj = self.env['account.analytic.account']
        start_date = fields.Date.context_today(self)
        for rec in self:
            bank_balance = 0
            today_balance = 0
            branch_id = rec.branch_id
            if rec.journal_id:            
                sql_query = ''
                if branch_id:
                    analytic_branch = aa_obj.search([('segmen','=',3),('branch_id','=',branch_id.id),('type','=','normal'),('state','not in',('close','cancelled'))])
                    analytic_cc = aa_obj.search([('segmen','=',4),('type','=','normal'),('state','not in',('close','cancelled')),('parent_id','child_of',analytic_branch.ids)])
                    sql_query = ' AND l.analytic_account_id in %s' % str(tuple(analytic_cc.ids)).replace(',)', ')')
                bank_balance = rec.journal_id.default_credit_account_id.with_context(date_from=start_date, date_to=start_date, initial_bal=True,sql_query=sql_query).balance or rec.journal_id.default_debit_account_id.with_context(date_from=start_date, date_to=start_date, initial_bal=True,sql_query=sql_query).balance
                today_balance = rec.journal_id.default_credit_account_id.with_context(date_from=start_date, date_to=start_date, initial_bal=False,sql_query=sql_query).balance or rec.journal_id.default_debit_account_id.with_context(date_from=start_date, date_to=start_date, initial_bal=False,sql_query=sql_query).balance
            rec.balance = bank_balance + today_balance
        return True

    @api.model
    def create(self,vals,context=None):

        if vals['amount']>self.balance:
            raise osv.except_osv(('Perhatian !'), ("Nilai amount tidak boleh lebih besar dari balance."))

        ....................................
        ...................................

        return value

在覆盖的 create 方法中,当我比较字段 amountbalance 时, balance 值为 False。它不是 compute=_get_balance 方法中的值

nb:对不起我的英语:)

在 create self 中只是一个空模型,所以每个字段都应该是 False:

试试这个:

 @api.model
 def create(self):
    rec = super(YourClass, self).create(vals)
    # rec is a record
    if rec.amount > rec.balance:
        raise osv.except_osv(('Perhatian !'), ("Nilai amount tidak boleh lebih besar dari balance."))
    return rec

但我认为对这类问题使用约束更好:

# no need to override create at all
@api.constrains('amount', 'balance')
def check_amount(self):
    for rec in self:
        if rec.amount > rec.balance:
            raise osv.except_osv(('Perhatian !'), ("Nilai amount tidak boleh lebih besar dari balance."))