Feng’ Blog

Odoo12添加附件的方式
Odoo12添加附件的方式首先安装Odoo自带模块在应用里搜索附件列出以及文档索引,安装模块即可方法一:在 mod...
扫描右侧二维码阅读全文
19
2019/05

Odoo12添加附件的方式

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 type="object" name="action_view_order" class="oe_stat_button"
    icon="fa-truck" attrs="{'invisible':[('state','!=','02')]}">
        <field name="purchase_order_num" widget="statinfo" string="采购订单" help="采购订单"/>
    </button>
</div>

效果图:

fujain4.png

fujian5.png

Last modification:May 19th, 2019 at 11:10 am
If you think my article is useful to you, please feel free to appreciate

2 comments

  1. 张先生

    楼主的博客怎么搭的,感觉很特别,方便说一下嘛

    1. 博主
      @张先生

      一个主题而已: https://www.ihewro.com/archives/489/

Leave a Comment