我有一个旧的odoo版本(V6),我正在将它迁移到odoo-10,我面临的问题是二进制域数据迁移。因为odoo-10有"attachment=True“属性,但是对于较老的版本没有这个属性。因此,我能否从堆栈社区获得一点想法,了解如何完成任务以及如何将postgres表迁移到odoo-10兼容数据。提前谢谢。
发布于 2019-04-15 19:46:55
只需按原样迁移数据,让它们存在于数据库中。我不得不写一个模块来实现同样的需求,因为客户在数据库中有附件,而不是使用附件。
下面的代码可以工作,它不是官方的在我的公司的应用程序中的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才能使用此方法。
发布于 2019-04-13 01:49:31
如果您查看二进制类(<path_to_v12>/odoo/fields.py lines 1786-1800,下面引用)的read函数,您会注意到它在ir.attachment中搜索具有正确的模型、字段和id的记录。
def read(self, records):
# values are stored in attachments, retrieve them
assert self.attachment
domain = [
('res_model', '=', records._name),
('res_field', '=', self.name),
('res_id', 'in', records.ids),
]
# Note: the 'bin_size' flag is handled by the field 'datas' itself
data = {att.res_id: att.datas
for att in records.env['ir.attachment'].sudo().search(domain)}
cache = records.env.cache
for record in records:
cache.set(record, self, data.get(record.id, False))因此,我有根据的猜测是,您可以更新'ir_attachment‘记录并添加res_model (请注意,这是一个字符串!)、res_field (也是一个字符串)和res_id (这是保存在引用记录的id字段中的整数)。
发布于 2019-04-24 19:25:43
最好是使用XMLRPC从SRC读取数据并将数据写入DEST。这会解决你的问题。它将从二进制字段中读取数据,同时创建将存储在文件系统中的附件。
https://stackoverflow.com/questions/55648822
复制相似问题