使用 XML-RPC 将销售从 xl 迁移到 odoo

Migrating sales from xl to odoo using XML-RPC

我正在尝试使用 xmlrpc 将销售订单从 excel sheet 迁移到 odoo 到目前为止,我的产品正在运行,客户端正在运行,但是当我尝试插入销售订单时,出现此错误

Traceback (most recent call last):
  File "/home/oasis/PycharmProjects/somig/migrator.py", line 75, in <module>
    'validity_date':"2016-01-18",
  File "/usr/lib/python2.7/xmlrpclib.py", line 1243, in __call__
    return self.__send(self.__name, args)
  File "/usr/lib/python2.7/xmlrpclib.py", line 1602, in __request
    verbose=self.__verbose
  File "/usr/lib/python2.7/xmlrpclib.py", line 1283, in request
    return self.single_request(host, handler, request_body, verbose)
  File "/usr/lib/python2.7/xmlrpclib.py", line 1316, in single_request
    return self.parse_response(response)
  File "/usr/lib/python2.7/xmlrpclib.py", line 1493, in parse_response
    return u.close()
  File "/usr/lib/python2.7/xmlrpclib.py", line 800, in close
    raise Fault(**self._stack[0])
xmlrpclib.Fault: <Fault 1: 'Traceback (most recent call last):\n  File "/usr/lib/python2.7/dist-packages/openerp/service/wsgi_server.py", line 56, in xmlrpc_return\n    result = openerp.http.dispatch_rpc(service, method, params)\n  File "/usr/lib/python2.7/dist-packages/openerp/http.py", line 114, in dispatch_rpc\n    result = dispatch(method, params)\n  File "/usr/lib/python2.7/dist-packages/openerp/service/model.py", line 37, in dispatch\n    res = fn(db, uid, *params)\n  File "/usr/lib/python2.7/dist-packages/openerp/service/model.py", line 173, in execute_kw\n    return execute(db, uid, obj, method, *args, **kw or {})\n  File "/usr/lib/python2.7/dist-packages/openerp/service/model.py", line 118, in wrapper\n    return f(dbname, *args, **kwargs)\n  File "/usr/lib/python2.7/dist-packages/openerp/service/model.py", line 181, in execute\n    res = execute_cr(cr, uid, obj, method, *args, **kw)\n  File "/usr/lib/python2.7/dist-packages/openerp/service/model.py", line 170, in execute_cr\n    return getattr(object, method)(cr, uid, *args, **kw)\n  File "/usr/lib/python2.7/dist-packages/openerp/api.py", line 250, in wrapper\n    return old_api(self, *args, **kwargs)\n  File "/usr/lib/python2.7/dist-packages/openerp/api.py", line 354, in old_api\n    result = method(recs, *args, **kwargs)\n  File "/usr/lib/python2.7/dist-packages/openerp/addons/sale/sale.py", line 232, in create\n    result = super(SaleOrder, self).create(vals)\n  File "/usr/lib/python2.7/dist-packages/openerp/api.py", line 248, in wrapper\n    return new_api(self, *args, **kwargs)\n  File "/usr/lib/python2.7/dist-packages/openerp/addons/mail/models/mail_thread.py", line 233, in create\n    thread = super(MailThread, self).create(values)\n  File "/usr/lib/python2.7/dist-packages/openerp/api.py", line 248, in wrapper\n    return new_api(self, *args, **kwargs)\n  File "/usr/lib/python2.7/dist-packages/openerp/models.py", line 4157, in create\n    record = self.browse(self._create(old_vals))\n  File "/usr/lib/python2.7/dist-packages/openerp/api.py", line 248, in wrapper\n    return new_api(self, *args, **kwargs)\n  File "/usr/lib/python2.7/dist-packages/openerp/api.py", line 490, in new_api\n    result = method(self._model, cr, uid, *args, **old_kwargs)\n  File "/usr/lib/python2.7/dist-packages/openerp/models.py", line 4301, in _create\n    tuple([u[2] for u in updates if len(u) > 2])\n  File "/usr/lib/python2.7/dist-packages/openerp/sql_db.py", line 141, in wrapper\n    return f(self, *args, **kwargs)\n  File "/usr/lib/python2.7/dist-packages/openerp/sql_db.py", line 220, in execute\n    res = self._obj.execute(query, params)\nProgrammingError: column "partner_id" is of type integer but expression is of type integer[]\nLINE 1: ...1, NULL, \'draft\', 6, 1, 1, \'2016-09-03 16:50:24\', ARRAY[6], ...\n                                                             ^\nHINT:  You will need to rewrite or cast the expression.\n\n'>

Process finished with exit code 1

我的代码如下

import psycopg2
import psycopg2.extras
import pyexcel_xls
import pyexcel as pe
from pyexcel_xls import get_data
from datetime import datetime
import xmlrpclib
import json

url = 'http://localhost:8070'
db = 'fresh'
username = 'admin'
password = 'odoo'
#data = get_data("salesorder.xls")
#print(json.dumps(data))
records = pe.get_records(file_name="salesorder.xls")
for record in records:
    print record['name']
    names = record['name']
    print record['location']
    print record['zip']
    print record['republic']
    dates = record['date']
    print dates
    print datetime.strptime(dates,'%d/%M/%Y')
    lastdat=datetime.strptime(dates,'%d/%M/%Y')
    print record['product']
    productname= record['product']


#Check if the customer is in or else insert him
    common = xmlrpclib.ServerProxy('{}/xmlrpc/2/common'.format(url))
    uid = common.authenticate(db, username, password, {})
    output = common.version()
    models = xmlrpclib.ServerProxy('{}/xmlrpc/2/object'.format(url))
    partnerids = models.execute_kw(db, uid, password,
                    'res.partner', 'search', [[['name', '=', record['name']]]])

    if partnerids:
        print partnerids

    else:
        newpartn = models.execute_kw(db, uid, password, 'res.partner', 'create', [{
            'name': names,
        }])
        partnerids=newpartn
        print partnerids
#Check if a product is in else insert a new product
    common = xmlrpclib.ServerProxy('{}/xmlrpc/2/common'.format(url))
    uid = common.authenticate(db, username, password, {})
    output = common.version()
    models = xmlrpclib.ServerProxy('{}/xmlrpc/2/object'.format(url))
    productids = models.execute_kw(db, uid, password,
                                   'product.product', 'search', [[['name', '=', record['product']]]])

    if productids:
        print productids

    else:
        newproduct = models.execute_kw(db, uid, password, 'product.product', 'create', [{
            'name': productname,
            'default_code': partnerids
        }])
        productids=newproduct
        print productids



    uid = common.authenticate(db, username, password, {})
    print output
    models = xmlrpclib.ServerProxy('{}/xmlrpc/2/object'.format(url))
    id = models.execute_kw(db, uid, password, 'sale.order', 'create', [{
        'partner_id': partnerids,
     #   'name': names,
        'validity_date':"2016-01-18",
        #'payment_term_id':1,
        # 'user_id':"1"
        # 'state':"sale"


    }])
    print id

第75行是有效日期

我认为问题在于您执行了对 partnerids 的搜索,这将导致一个数组。但是,如果您没有找到任何您创建一个由整数表示的合作伙伴。然后,您将 partnerids 分配给 sale.order 中的 partner_id。

有时您为它分配一个整数,有时为一个数组,这取决于您使用的是来自搜索的合作伙伴 ID 还是新创建的合作伙伴。

如果您确定您对 partnerids 的搜索绝不会导致两个合作伙伴具有相同的名称,那么您可以使用我在下面显示的搜索中返回的第一条记录。我在第二行重新分配了 partnerids 只是为了说明差异。

如果您不确定自己是否有重复项,这将是错误的做法,您应该将搜索优化到 select 正确的合作伙伴。

    partnerids = models.execute_kw(db, uid, password,
                'res.partner', 'search', [[['name', '=', record['name']]]])
    partnerids = partnerids[0] if partnerids else False 

if partnerids:
    print partnerids

else:
    newpartn = models.execute_kw(db, uid, password, 'res.partner', 'create', [{
        'name': names,
    }])
    partnerids=newpartn

错误消息说:

ProgrammingError: column "partner_id" is of type integer but expression is of type integer[].

只需将 partnerids 作为整数输入即可:

if partnerids:
   partnerids = partnerids[0]
   print partnerids