odoo12附件上传方式

odoo附件上传的方式

1.下载 odoo 自带模块

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

2.在模型类中和视图中添加相应代码

  • 方式一:

在模型中添加如下代码

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

注意: 将代码中对应红框中的模型改为你自己的模型名字即可

odoo12附件上传方式_第1张图片

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>
  • 方式二:

在模型中添加如下代码

@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>

你可能感兴趣的:(odoo)