odoo 附件上传的三种姿势
odoo 附件上传的三种姿势
因公司业务需要实现上传附件功能,一番折腾找到了三种附件上传的姿势,不过好像用处不大。。。。
1. 下载 odoo 自带模块
在应用里搜索附件列出以及文档索引,安装模块即可
这样在编辑界面就可以看见附件上传按钮,上传即可
2.代码按钮上传第一种方式
在 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', '=', 'cms_su'), ('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', '=', 'cms_su'), ('res_id', 'in', self.ids)]
res['context'] = {'default_res_model': 'cms_su', 'default_res_id': self.id}
return re
在 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>
在视图中就可以显示按钮,实现附件上传
3.代码按钮上传第二种方式
在 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">
<!--<field name="img_count" widget="statinfo"/>-->
<div class="o_stat_info">
<span class="o_stat_text">附件管理</span>
</div>
</button>
</div>
在视图会显示按钮,同样可以实现附件上传