目录
1、上传图片
odoo中有一个product.image模型,继承该模型可以实现多张图片的上传和预览。实现效果如下图所示:
python代码:
# -*- coding: utf-8 -*-
from odoo import api, fields, models
class ProductImage(models.Model):
_inherit = 'product.image'
cooperation_id = fields.Many2one("market.cooperation")
xml代码:
<page string="成品文件">
<field name="finished_file_ids" class="o_website_sale_image_list" mode="kanban"
options="{'create_text':'Add an Image'}" nolabel="1"/>
</page>
其中finished_file_ids这个字段是一个一对多字段,关联的就是之前所继承的product.image模型。
因为该模型使用的是fields.Image字段,所以只能实现图片的上传,当需要上传其他附件的时候该方法就不可行
2、上传不同附件
实现效果如下图所示:
通过点击添加附件按钮,将附件添加到ir.attachment模型 ,然后点击右上角的合约文件就可以看到相应的文件。
实现方式:
在header标签内定义左边按钮组件:
<widget name="attach_document" string="Attach Document" action="message_post"/>
视图右边按钮:
<button name="action_get_attachment_view" class="oe_stat_button" icon="fa-file-text-o"
type="object">
<field name="attachment_number" widget="statinfo" string="合约文件"
options="{'reload_on_button': true}"/>
</button>
其中attachment_number是一个计算字段,计算ir.attachment中这个模型的附件数量:
def _compute_attachment_number(self):
attachment_data = self.env['ir.attachment'].read_group(
[('res_model', '=', 'market.cooperation'), ('res_id', 'in', self.ids)], ['res_id'], ['res_id'])
attachment = dict((data['res_id'], data['res_id_count']) for data in attachment_data)
for record in self:
record.attachment_number = attachment.get(record.id, 0)
action_get_attachment_view 是点击右边按钮所调用的方法,可以自行添加domain和context对返回的视图数据过滤,例如:
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', '=', 'market.cooperation'), ('res_id', 'in', self.ids)]
res['context'] = {'default_res_model': 'market.cooperation', 'default_res_id': self.id}
return res
3、page页面添加附件看板
可以通过继承ir.attachment模型,写一个一对多字段
4、如何在一个视图里面多个page页面添加文件?
并且是图片的话将它预览
这时候就需要我们自己定义模型,如果继承ir.attachment的话就会到时这几个page页的文件是共用的,以上为成品文件和合约文件分别定义模型,并且为这两个模型添加看板视图和form视图,看板视图用于文件在page页面的显示,form视图用于添加文件时的显示。
其中一个模型定义如下,大家可以参照原生的ir.attachment模型是如何定义的:
class MarketDocument(models.Model):
_name = 'market.document'
# 文件名
filename = fields.Char('Name')
# 文件
document = fields.Binary(string='File Content', )
mimetype = fields.Char('Mime Type', readonly=True, compute='_compute_mimetype')
url = fields.Char('Url', index=True, size=1024)
type = fields.Selection([('url', 'URL'), ('binary', 'File')],
string='Type', required=True, default='binary', change_default=True,
help="You can either upload a file from your computer or copy/paste an internet link to your file.")
cooperation_id = fields.Many2one("market.cooperation")
上图的按钮定义如下:
<field name="finished_file_ids" mode="kanban"
options="{'create_text':'Add an File'}" nolabel="1">
</field>
form视图定义如下:
<record id="action_marking_cooperation_document_form" model="ir.ui.view">
<field name="name">cooperation.document.form</field>
<field name="model">market.document</field>
<field name="arch" type="xml">
<form>
<sheet>
<group>
<field name="filename"/>
<field name="document" widget="binary" filename="filename"/>
<field name="type"/>
</group>
</sheet>
</form>
</field>
</record>
其中的 filename属性是为了获取文件上传时候的文件名赋值给filename字段。
实现图片预览和布局就需要写相应的看板视图,这里就不多说了....