首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用attachment=True将旧的Odoo二进制域迁移到新的odoo版本?

如何使用attachment=True将旧的Odoo二进制域迁移到新的odoo版本?
EN

Stack Overflow用户
提问于 2019-04-12 17:43:40
回答 3查看 939关注 0票数 0

我有一个旧的odoo版本(V6),我正在将它迁移到odoo-10,我面临的问题是二进制域数据迁移。因为odoo-10有"attachment=True“属性,但是对于较老的版本没有这个属性。因此,我能否从堆栈社区获得一点想法,了解如何完成任务以及如何将postgres表迁移到odoo-10兼容数据。提前谢谢。

EN

回答 3

Stack Overflow用户

发布于 2019-04-15 19:46:55

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

下面的代码可以工作,它不是官方的在我的公司的应用程序中的Odoo的应用程序商店,但最终会找到它的方式;-)

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2019-04-13 01:49:31

如果您查看二进制类(<path_to_v12>/odoo/fields.py lines 1786-1800,下面引用)的read函数,您会注意到它在ir.attachment中搜索具有正确的模型、字段和id的记录。

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2019-04-24 19:25:43

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55648822

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档