只需原样迁移数据,让它们存在于数据库中。我必须编写一个模块来实现相同的要求,因为客户在数据库中有附件而不是使用附件。
以下代码有效,它不在我公司在 Odoo 应用商店的应用程序中,但最终会找到它的方式 ;-)
from odoo import api, models, exceptions
from odoo.osv import expression
class IrAttachment(models.Model):
""" Attachment Extensions"""
_inherit = 'ir.attachment'
@api.model
def _relocate_binary_data(
self, model=None, fields=None, domain=None, limit=0):
""" Relocates binary data into attachments. This method
has no functionality to reverse the process.
Use this to change binary fields to attachment usage,
which is done by using the parameter attachment=True
@param model: Model Name (required)
@param fields: List of binary field names (required)
@param domain: optional search domain to filter treated records
(default: []==no filter)
@param limit: optional filter limit (default: 0==unlimited)"""
if not model or not fields:
raise exceptions.Warning(
"model and fields are required parameters")
# only touch records with binary data in one of the provided fields
default_domain = [[(f, '!=', False)] for f in fields]
default_domain = expression.OR(default_domain)
domain = expression.AND([domain, default_domain])
records = self.env[model].with_context(active_test=False).search(
domain, limit=limit)
# relocate the binary data to attachments
for record in records:
for field in fields:
# search for existing attachments (for re-runs)
attachment = records.env['ir.attachment'].sudo().search([
('res_model', '=', record._name),
('res_field', '=', field),
('res_id', '=', record.id),
])
# write the binary value to existing attachment or create one
if attachment:
attachment.write({'datas': getattr(record, field)})
else:
self.env['ir.attachment'].create({
'name': record.name,
'res_model': record._name,
'res_field': field,
'res_id': record.id,
'type': 'binary',
'datas': getattr(record, field)
})
# empty the database binary data
records.write({f: None for f in fields})
您必须编写ir.cron 或ir.actions.server 才能使用此方法。