odoo里面批量上传图片

import os
import base64


def base_data_product_image(self):
    """
    odoo里批量创建产品,并上传图片
    图片为binary类型字段
    :param self:
    :return:# odoo里面附近存储格式三base64编码格式的
    """
    path = "D:\image_files"  # 这里windows环境路径
    filelist = os.listdir(path)
    product_obj = self.env['product.template'].sudo()
    i = 1
    if not len(filelist):
        return {
            "result": "Fail",
            "msg": "not image!"
        }
    for image in filelist:
        # 这里可以根据后缀来过滤文件类型jpg、png
        if image.endswith('.png'):
            with open("D:\image_files\%s" % image, "rb") as f:
                base64_data = base64.b64encode(f.read())
            product_obj.create({
                "name": "test_product_%s" % str(i),
                "image_1920": base64_data
            })
            # 文件打开后记得close,
            # 1、with open()as f;文件打开后会自动关闭
            # 2、open(),打开文件不会自动关闭
            # 建议都close一下
            f.close()
            i += 1
    return {
        "result": "Success",
        "msg": "Product create success!"
    }



# 案例2
# 这是针对many2many类型的ir.attachment字段创建的例子
name="test"
data_attach = {
    'name': name,
    'datas': base64.b64encode(content),
    'type': 'binary',
    'datas_fname': name,
    'description': name,
    'res_model': 'test_model',
    'res_id': '1',
}
new_attachment = IrAttachment.create(data_attach)
原文地址:https://www.cnblogs.com/1314520xh/p/14256662.html