我有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
型号的所有附件的计数(Delivery Nots、Slips、Receipts等(。
因此,您需要添加一个可以存储的计算字段,但很难触发对该字段的重新计算,因为附件与记录间接相关(数据库中没有使用真正的外键(。
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', '=', picking.id),
('res_model', '=', self._name)]
picking_count = self.search_count(domain)
if picking.move_lines:
domain_move = [('res_id', 'in', picking.move_lines.ids),
('res_model', '=', 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)