ir_attachment attachment system management attachment
odoo files are stored here
The storage path is
add_dir = os.path.join(self[‘data_dir’], ‘addons’)
中
Main structural parameters
Field | Types of | Description |
---|---|---|
res_id | integer | Owning record |
res_model | char | Affiliation model |
name | char | Attachment display name |
datas_fname | char | accessory name |
dates | binary | base64 encoded file |
store_fname | char | Soft address for attachment storage |
_filestore() | function | Returns the actual storage path of the attachment |
type | selection | url/binary |
url | char | url connection address |
db_datas | binary | If it is not a storage database, storage entity |
companyjd | Many2one | Default company |
description | text | Introduction |
The default xml location is'base'.'action_attachment'
<field name="name"/>
<field name="folder_id"/>
<field name="tag_ids" widget="many2many_tags"/>
<field name="datas" filename="datas_fname" attrs="{'invisible':[('type','=','url')]}"/>
<field name="datas_fname" invisible="1" attrs="{'invisible':[('type','=','url')]}" class="oe_inline oe_right"/>
<field name="owner_id"/>
<field name="partner_id"/>
<field name="file_size" attrs="{ 'invisible' : [('type', '!=', 'binary')]}"/>
<field name="type" readonly="1"/>
<field name="url" attrs="{ 'invisible' : [('type', '!=', 'url')]}"/>
Upload method
- Use the uploaded binary data to upload to python. Then use python's ability to write to the file
datas = fields.Binary(string='File Content', compute='_compute_datas', inverse='_inverse_datas')
store_fname = fields.Char('Stored Filename')
def _inverse_datas(self):
for attach in self:
# compute the fields that depend on datas
value = attach.datas
bin_data = base64.b64decode(value) if value else b''
vals = {
'file_size': len(bin_data),
'checksum': self._compute_checksum(bin_data),
'index_content': self._index(bin_data, attach.datas_fname, attach.mimetype),
'store_fname': False,
'db_datas': value,
}
# 写入二进制文件
if value and location != 'db':
# save it to the filestore
vals['store_fname'] = self._file_write(value, vals['checksum'])
vals['db_datas'] = False
#......
# 获取所有路径
@api.model
def _full_path(self, path):
# sanitize path
path = re.sub('[.]', '', path)
path = path.strip('/\\')
return os.path.join(self._filestore(), path)
How to upload file name
filename: Used for binary fields, it is the model field name used to store the name of the uploaded file.
demo does not use field attributes to upload files
https://sxfblog.com/index.php/archives/421.html
In fact, the principle is found. You can use other methods
such as line_ids = one2many('ir.attachment')
and write ir.attachment to get the tree and form fields.