Просто перенесите данные как есть, пусть они существуют в базе данных. Мне пришлось написать модуль, чтобы выполнить то же требование, потому что у клиента были вложения в базе данных вместо использования вложения.
Следующий код работает, он не является официальным в приложениях моей компании в магазине приложений 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
, чтобы использовать этот метод.