当前位置:网站首页>odoo14 | 附件上传功能及实际使用
odoo14 | 附件上传功能及实际使用
2022-07-31 09:49:00 【埃尔文编程实验室】
有时候,有的需求会要求在表单中增加一个附件上传的要求,就像下面一样

这个其实odoo官方已经写了这个功能,里面可以上传附件也可以
但需要注意附件上传按钮只有必填项填完后才能点击进去进行上传附件动作

上传附件后,其他用户在查看这一记录时,就能通过右上角的上传附件按钮进去查看该记录所附带的附件内容(需要下载到本地查看)了。
实现过程
首先在字段下添加对应的方法
# 附件上传
def _compute_attachment_number(self):
"""附件上传"""
attachment_data = self.env['ir.attachment'].read_group([
('res_model', '=', '你当前的模型名_name的内容'),
('res_id', 'in', self.ids)], ['res_id'], ['res_id'])
attachment = dict((data['res_id'], data['res_id_count']) for data in attachment_data)
for doc in self:
doc.attachment_number = attachment.get(doc.id, 0)
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', '=', '你当前的模型名_name的内容'), ('res_id', 'in', self.ids)]
res['context'] = {'default_res_model': '你当前的模型名_name的内容', 'default_res_id': self.id}
return res这是odoo14的固定写法,两个配套的方法,只需修改代码中中文提示的部分,替换成你的模型名_name='odoo.study'中的odoo.study,即可使这两个方法生效。
之后进入xml视图中,在form视图中的<sheet>标签中
<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>增加这几行代码来显示附件上传的按钮
然后重启服务并升级模块,即可查看效果。
边栏推荐
猜你喜欢

如何判断自己是否适合IT行业?方法很简单

来n遍剑指--06. 从尾到头打印链表

Open Kylin openKylin automation developer platform officially released

Kotlin—基本语法(三)

数据中台建设(六):数据体系建设

二叉树的搜索与回溯问题(leetcode)

The fifth chapter

Browser usage ratio js radar chart

Canvas particles change various shapes js special effects

js right dot single page scrolling introduction page
随机推荐
loadrunner脚本--添加集合点
js implements the 2020 New Year's Day countdown bulletin board
恋爱期间的赠与能否撤销
js以变量为键
【职场杂谈】售前工程师岗位的理解杂谈
OpenGL es 初识
学习笔记——七周成为数据分析师《第二周:业务》:业务分析框架
NowCoderTOP17-22 Binary search/sort - continuous update ing
Redis Cluster - Sentinel Mode Principle (Sentinel)
Solve rpc error: code = Unimplemented desc = method CheckLicense not implemented
Canvas particles change various shapes js special effects
ReentrantLock
Qt 编译错误:C2228: “.key”的左边必须有类/结构/联合
Come n times - 07. Rebuild the binary tree
loadrunner-controller-view script与load generator
Come n times - 09. Implement queues with two stacks
一次Spark SQL线上问题排查和定位
第七章
来n遍剑指--07. 重建二叉树
Gradle series - Groovy overview, basic use (based on Groovy document 4.0.4) day2-1