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

时间:2019-04-12 09:43:40

标签: postgresql odoo odoo-8 odoo-10 odoo-9

我有一个旧的odoo版本(v6),正在将其迁移到odoo-10,我面临的问题是二进制字段数据迁移。由于odoo-10的属性为“ attachment = True”,但对于较早的版本,此属性不存在。 因此,我对堆栈社区一无所知,关于如何完成任务以及如何将Postgres表迁移到odoo-10兼容数据。预先感谢。

3 个答案:

答案 0 :(得分: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(也是一个字符串)和{{1} }(这是保存在引用记录的res_id字段中的整数)。

答案 1 :(得分: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才能使用此方法。

答案 2 :(得分:0)

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