odoo 8 计算附件并在列表视图中显示

问题描述

我有 odoo 8。我想从 ir_attachment 计算附件并将其显示在 stock.production.lot 中。这是我的 .py

class stock_production_lot(models.Model):
_inherit='stock.production.lot'

@api.multi
def get_attachment_info(self):
    for lot in self:
        so_line_ids = self.env['ir.attachment'].search([('res_id','=',lot.id)])
        for pick in so_line_ids:
            pick.count_attachment = 0
            if pick.datas_fname:
                pick.count_attachment = len(pick.datas_fname)

count_attachment = fields.Float(string='Total Attachment',compute='get_attachment_info')

这是视图

<field name="count_attachment" />

谢谢

解决方法

很难回答,因为你问题中的信息有点少。但是让我试着用一个一般的例子来以一般的方式回答它,我会怎么做。

假设您想要对模型 stock.picking 的所有附件(交货单、单据、收据等)进行计数。

所以你需要添加一个计算字段,它可以存储,但是很难触发这个字段的重新计算,因为附件与记录间接相关(数据库中没有使用真正的外键)。

class StockPicking(models.Model):
    _inherit = 'stock.picking'

    attachment_count = fields.Integer(compute="_compute_attachment_count")

    @api.multi
    def _compute_attachment_count(self):
        for picking in self:
            domain = [('res_id','=',picking.id),('res_model',self._name)]
            picking.attachment_count = self.search_count(domain)

并将新字段添加到模型 stock.picking 的视图中。

现在让我们假设您不仅需要采摘的附件,还需要它们的移动线。 只需“计数”它们并将该计数添加到前一个:

    @api.multi
    def _compute_attachment_count(self):
        for picking in self:
            domain = [('res_id',self._name)]
            picking_count = self.search_count(domain)
            if picking.move_lines:
                domain_move = [('res_id','in',picking.move_lines.ids),picking.move_lines._name)]
                picking_count += picking.move_lines.search_count(domain_move)
            picking.attachment_count = picking_count
,

感谢您的回复。我有答案了

@api.one
def count_attachments(self):
    obj_attachment = self.env['ir.attachment']
    for record in self:
        record.count_attachment = 0
        attachment_ids = obj_attachment.search([('res_model',self._name),('res_id',record.id)])
        if attachment_ids:
            record.count_attachment = len(attachment_ids)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...