如何将Odoo旧二进制字段迁移到attachment = True的新odoo版本?

问题描述 投票:0回答:3

我有一个旧的odoo版本(v6),我正在将它迁移到odoo-10,我面临的问题是二进制字段数据迁移。由于odoo-10具有属性“attachment = True”,但对于旧版本,这并不存在。因此,我可以从堆栈社区了解一下,如何实现我的任务以及如何将该postgres表迁移到odoo-10兼容数据。提前致谢。

postgresql odoo odoo-8 odoo-10 odoo-9
3个回答
1
投票

如果你看一下Binary类的读取函数(<path_to_v12>/odoo/fields.py lines 1786-1800,下面引用),你会注意到它在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字段上的整数)。


1
投票

只需按原样迁移数据,让它们存在于数据库中。我必须编写一个模块来实现相同的要求,因为客户在数据库中有附件而不是使用附件。

以下代码有效,它不是我公司在Odoo App Store中的应用程序正式,但最终会找到它的方式;-)

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.cronir.actions.server来使用这种方法。


0
投票

最好是使用XMLRPC从SRC读取数据并在DEST中写入数据。哪会解决你的问题。它会在创建存储在文件系统中的附件时读取二进制字段中的数据。

© www.soinside.com 2019 - 2024. All rights reserved.