3

根据这个论坛功能领域

“在调用创建、写入或任何其他 orm 方法时调用功能字段”

我已经测试了这个想法,我创建了一个函数字段,并且每次我(1)在列表/树上添加新项目或(2)编辑列表/树上的现有项目时都会调用它

但我想知道是否有办法在用户从树中删除项目时调用/调用功能字段?

class payment(osv.osv):

    def _get_total_check_info(self, cr, uid, ids, field, arg, context=None):
        """
        _get_total_check_info() :
        Function that automatically computes for the Total Check Info upon saving
        """
        result={}
        check_amount = 0
        for record in self.browse(cr, uid, ids, context=context):
            for n in record.check_info_ids:
                res = self.pool.get('cashier.check.information').browse(cr, uid, n.id)
                if res:
                    check_amount +=  res['check_amount']
                    print "check_amount", res['check_amount']
                    print "check_amount Total", check_amount

        result[record.id] = check_amount
        return result

        _name = 'payment'
        _description = __doc__

        _columns = {
       'check_info_ids': fields.one2many('check.information', 'pymt_id', 'Check Information'),
       'total_check_amount': fields.function(_get_total_check_info, method=True, type='float', string='Total Check Amount', store=True),
        }
payment()


class check_information(osv.osv):

    def unlink(self, cr, uid, ids, context=None):
        """
        unlink():
        Function used to override the unlink method
        """
        #Your code goes here.
        print "here at unlink function"
        return super(check_information, self).unlink(cr, uid, ids, context=context)

    _name = 'check.information'
    _description = __doc__

    _columns = {
        'pymt_id': fields.many2one('payment', 'Payment ID'),
        'check_date': fields.date('Check Date'),
        'check_number': fields.char('Check Number', size=50),
        'check_amount': fields.float('Check Amount', digits=(16, 2)),
        'payee': fields.char('Payee', size=100),
        'bank': fields.char('Bank', size=20),
        'bank_branch': fields.char('Bank Branch', size=20),
    }
check_information()
4

2 回答 2

4

通过对象,您可以通过创建对象来调用功能字段的方法

def unlink(self, cr, uid, ids, context=None):
    """
    unlink():
    Function used to override the unlink method
    """
    #Your code goes here.
    lst_ids = []
    for brw_rec in self.browse(cr, uid, ids, context=context):
        print "here at unlink function"
        lst_ids.append(brw_rec.pymt_id.id)
    self.pool.get('payment')._get_total_check_info(cr, uid, lst_ids, 'total_check_amount', None, context=context)
    return super(check_information, self).unlink(cr, uid, ids, context=context)
于 2013-01-03T13:15:58.113 回答
3

是的,fields.functions只有当我们尝试更新或创建记录时才会调用。但是如果你想做你想做的事,那么形成覆盖unlink方法`然后形成self_。列找到您的字段并获取函数名称,然后调用方法。通过这样做,您将模拟 ORM 在 crate 或 write 上所做的确切行为。

class test(osv.osv):
    .
    .
    .
    def unlink(self, cr, uid, ids, context=None):
        #Your code goes here.
        return super(test, self).unlink(cr, uid, ids, context=context)

谢谢

于 2013-01-03T04:58:13.383 回答