0

我需要计算 one2many 中的记录数,我可以使用以下方法来做到这一点:

def update_cust(self,cr,uid,ids,context=None):
    current_obj = self.browse(cr,uid,ids[0])
    one2many_ids=self.pool.get('customer.test').search(cr,uid,[], count=True)

    if one2many_ids==0:
        one2many_ids=1
        count=one2many_ids
    elif one2many_ids<11:
        one2many_ids=one2many_ids+1
        count=one2many_ids
    elif one2many_ids==11:
        return True
    elif not one2many_ids:
        count=one2many_ids=11
    else:
        one2many_ids=12
        count=one2many_ids
    self.pool.get('customer.test').create(cr,uid,{
                            'cust_id':current_obj.id,
                            'customer':current_obj.customer.id,
                            'street':current_obj.street,
                            'phone':current_obj.phone,
                            'cust_address':current_obj.cust_address.id,
                            'code':count-1
                    })
    return True

尽管我需要做的是,如果取消链接记录并添加新记录,则下一条记录必须从 11 开始... & one2many 中的记录总数最多必须为 10 one2many 定义和类定义:

class customer_test(osv.osv):
    _name="customer.test"
    _columns={
            'customer':fields.many2one('res.partner','Customers',ondelete="cascade"),
            'cust_id':fields.many2one('customer.test','CustId'),
            'street':fields.char('Street',size=40),
            'phone':fields.integer('Phone'),
            'cust_address':fields.many2one('res.partner.address','Address'),
            'cust_address2':fields.many2one('res.partner.address','Secondary Address'),
            'cust_one2many':fields.one2many('customer.test','cust_id',''),
            'code' : fields.integer('Code'),
}
4

1 回答 1

0
    def update_cust(self,cr,uid,ids,context=None):
    current_obj = self.pool.get('customer.test').browse(cr,uid,ids[0])
    code=current_obj.code+1
    count=one2many_ids=self.pool.get('customer.test').search(cr,uid,[('cust_id','=',current_obj.id)], count=True)
    vals=self.pool.get('customer.test').search(cr,uid,[('id','=',current_obj.id)])
    sequence_obj = self.pool.get('customer.test')
    seq_id = sequence_obj.search(cr, uid, [('id', '=', current_obj.id)])   
    if count==0:
        sequence_obj.write(cr, uid, seq_id, {'code': 1})      
    while current_obj.id in vals and count < 10:
        sequence=self.pool.get('customer.test').browse(cr,uid,ids[0]).code
        self.pool.get('customer.test').create(cr,uid,{
                'cust_id':current_obj.id,
                'customer':current_obj.customer.id,
                'street':current_obj.street,
                'phone':current_obj.phone,
                'cust_address':current_obj.cust_address.id,
                'code':sequence})
        sequence_obj.write(cr, uid, seq_id, {'code': sequence+1})      
        return True
    return True

这就是我的问题的解决方案

于 2016-10-27T10:01:59.387 回答