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
注意: 將代碼中對應紅框中的模型改爲你自己的模型名字即可
在
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>