计算附件数量并在 openerp 7.0 的树视图中显示

Calculate no of attachments and show it in tree view in openerp 7.0

我正在使用以下代码在 stock.picking.in 对象中添加一个新列并更新它的附件数(以在树视图中显示附件数)。

class stock_picking(osv.osv):
_inherit = "stock.picking.in"
_name = 'stock.picking.in'
def count_attachments(self, cr, uid, ids, fields, arg, context):
    obj_attachment = self.pool.get('ir.attachment')
    for record in self:
        _logger.info("record now in tree view:"+record)
        record.attachment_count =0
        attachment_ids = obj_attachment.search([('res_model','=','stock.picking.in'),('res_id','=',record.id)]).ids
        if attachment_ids:
            record.attachment_count =len(attachment_ids)
    return record

_columns = {
            'attachment_count' :fields.function(count_attachments,method=True,string="Attachment Count" ,type='integer')
    }

stock_picking()

然后我在树视图中添加了以下行。

<field name="attachment_count">

在树视图中显示计数。

但是这些值没有得到更新并且 count_attachments 没有被调用。

感谢任何帮助。提前致谢!

尝试关注,

class stock_picking(osv.osv):
    _inherit = "stock.picking.in"
    _name = 'stock.picking.in'

    def count_attachments(self, cr, uid, ids, fields, arg, context=None):
        obj_attachment = self.pool.get('ir.attachment')
        res = {}
        for record in self:
            res[record.id] = 0
            _logger.info("record now in tree view:"+record)
            attachment_ids = obj_attachment.search([('res_model','=','stock.picking.in'),('res_id','=',record.id)]).ids
            if attachment_ids:
                res[record.id] = len(attachment_ids)
        return res

    _columns = {
                'attachment_count' :fields.function(count_attachments,method=True,string="Attachment Count" ,type='integer')
        }