0

在ticket.py中。我有两节课。类 deposit.line 和 res_partner (继承)。我想获取类 deposit.line 的日期,但它在类 res_partner(inherit) 中的功能

def _compute_dept2(self, cr, uid, ids, amount, arg, context=None):
    result = {}
    obj2 = self.pool.get('deposit.line')
    for record in obj2.deposit_line:

        temp1 = record.date
        print temp1 
        print result
    return result

但其现有打印结果为假。怎么了?请更正我的代码

PS:我的解释不太好。但看看我的代码,肯定知道我的解释。

这是我的完整代码:

class deposit_line(osv.osv):
    _name ="deposit.line"
    _description = "Deposit Line" 

    _columns = {
        'name': fields.char('Name', size=64),
        'ref': fields.char('Reference', size=64),
        'amount': fields.float('Amount'),
        'date': fields.date('Date', required=True),
        'deposit_id': fields.many2one('res.partner', 'Deposit ', required=True, ondelete='cascade'),
    }
deposit_line()


class res_partner(osv.osv):
    _inherit = 'res.partner'

    def _compute_age(self, cr, uid, ids,date_birth,age,arg, context=None):
        result = {}
        for r in self.browse(cr, uid, ids, context=context):
           age=0
           if r.date_birth:
                age = (datetime.now()-datetime.strptime(r.date_birth,"%Y-%m-%d")).days/365.25
           result[r.id] = age 
        return result

    def _compute_dept(self, cr, uid, ids, deposit, available, arg, context=None):
        result = {}
        for r in self.browse(cr, uid, ids, context=context):
           avail=0
           temp = r.available
           if r.deposit:
                avail = r.deposit + temp
           result[r.id] = avail
        return result

    def _compute_dept2(self, cr, uid, ids, amount, arg, context=None):
        result = {}
        obj2 = self.pool.get('deposit.line')
        for record in obj2.deposit_line:

        temp1 = record.date
        print temp1 
        print result
            return result

    _columns = {
        'speaker': fields.boolean('Leader'),
        'event_ids': fields.one2many('event.event','main_speaker_id', readonly=True),
        'event_registration_ids': fields.one2many('event.registration','partner_id', readonly=True),
        'airline': fields.boolean('Airlines'),
        'hotel': fields.boolean('Hotel'),
        'date_birth': fields.date('Date of Birth'),
        'id_no': fields.char('ID. No', size=20),
        'id_expired': fields.date('Expired Date'),
        'sex':fields.selection([('male','Male'),('female','Female')],'Sex'),
        'age' : fields.function(_compute_age, type='float', method=True, store=True, string='Age', readonly=True),
        'deposit': fields.function(_compute_dept2, type='float', method=True, store=True, string='Deposit', readonly=True),
        'available': fields.function(_compute_dept, type='float', method=True, store=True, string='Available', readonly=True),
        'deposit_ids':fields.one2many('deposit.line', 'deposit_id', 'Deposit Line'),

    }

res_partner()
4

2 回答 2

2

Since you have one2many field for deposit_line defined in the res.partner model, you do not need the to access deposit_line object directly.

def _compute_dept2(self, cr, uid, ids, amount, arg, context=None):
    result = {}
    for partner in self.browse(cr, uid, id, context=context)
        result[partner.id]=0
        for deposit_line in partner.deposit_ids:
            result[partner.id] += deposit_line.amount
    return result
于 2012-07-27T08:18:29.817 回答
1

在第 3 行之后,您忘记了:

obj2.browse(cr, uid, ids, context=context)

你应该学会使用调试器:

添加import pdb; pdb.set_trace()要放置断点的行。当 Python 到达断点时,它会在控制台处停止并(pdb)提示。可以输入p obj打印变量 obj、跳转n到下一条指令并h寻求帮助。您可能会在这篇文章文档中找到更多信息。

于 2012-07-27T07:11:58.830 回答