Odoo12添加附件的方式

首先安装Odoo自带模块

在应用里搜索附件列出以及文档索引,安装模块即可

fujian1.png

方法一:

在 model 类中添加如下代码

attachment_number = fields.Integer(compute='_compute_attachment_number', string='附件上传')

@api.multi
def _compute_attachment_number(self):
    """附件上传"""
    attachment_data = self.env['ir.attachment'].read_group(
        [('res_model', '=', 'purchase.contract'), ('res_id', 'in', self.ids)], ['res_id'], ['res_id'])
    attachment = dict((data['res_id'], data['res_id_count']) for data in attachment_data)
    for expense in self:
        expense.attachment_number = attachment.get(expense.id, 0)

@api.multi
def action_get_attachment_view(self):
    """附件上传动作视图"""
    self.ensure_one()
    res = self.env['ir.actions.act_window'].for_xml_id('base', 'action_attachment')
    res['domain'] = [('res_model', '=', 'purchase.contract'), ('res_id', 'in', self.ids)]
    res['context'] = {'default_res_model': 'purchase.contract', 'default_res_id': self.id}
    return res

fujian2.png

在 xml 视图文件添加如下代码

<div class="oe_button_box" name="button_box">
  <button name="action_get_attachment_view" class="oe_stat_button" icon="fa-book" type="object">
      <field name="attachment_number" widget="statinfo" string="附件上传"/>
  </button>
</div>

效果图:

fj3.png

方法二:

在 model 类中添加如下代码

@api.multi
def attachment_image_preview(self):
    """附件上传 第二种方式"""
    self.ensure_one()
    # domain可以过滤指定的附件类型 (mimetype)
    domain = [('res_model', '=', self._name), ('res_id', '=', self.id)]
    return {
        'domain': domain,
        'res_model': 'ir.attachment',
        'name': u'附件管理',
        'type': 'ir.actions.act_window',
        'view_id': False,
        'view_mode': 'kanban,tree,form',
        'view_type': 'form',
        'limit': 20,
        'context': "{'default_res_model': '%s','default_res_id': %d}" % (self._name, self.id)
    }

在 xml 视图文件添加如下代码

<div class="oe_button_box" name="button_box">
   <button name="attachment_image_preview" type="object" class="oe_stat_button" icon="fa-image">
         <div class="o_stat_info">
             <span class="o_stat_text">附件管理</span>
         </div>
   </button>
</div>

效果图:

fujain4.png

fujian5.png

Last modification:October 12th, 2019 at 09:41 pm
If you think my article is useful to you, please feel free to appreciate