1

我使用 python 瓶创建了一个用于访问 OpenERP REST API 的 api。

我的问题是在上传图像文件并将其写入 OpenERP 中的二进制字段时

它抛出 raise TypeError,“除非启用 allow_none,否则无法编组 None”

在这里我上传了我的代码

from bottle import get, post, run,request,error,route,template
@route('/')
def index():
   return'''Welcome'''


@error(500)
def custom500(error):
   return 'Error while validating data.'

# Advisor Creation Start #

@get('/advisor') # or @route('/advisor')

def advisor_form():
    form ='''<form method="POST" action="/advisor" enctype="multipart/form-data">
            Photo:<input name="photo" type="file"/><br>
            <input type="submit" />
          </form>'''
    return form

@post('/advisor') # or @route('/advisor', method='POST')
def advisor_submit():
   import xmlrpclib
   username = 'uname'
   pwd = 'pwd'      
   dbname = 'db'    

   photo  = request.files.get('photo')
   print photo,"L:K<:L"
   sock_common = xmlrpclib.ServerProxy ('http://localhost:8069/xmlrpc/common')
   try:
      uid = sock_common.login(dbname, username, pwd)
   except("Error username or password"):
      print "sock_common error"
   sock = xmlrpclib.ServerProxy('http://localhost:8069/xmlrpc/object')
   res={
     'consultant_photo':photo
   }

   advisor_id = sock.execute(dbname, uid, pwd, 'res.advisor', 'create', res)
   return 'Advisor Created!'
   # Advisor Creation End #
 run(host='localhost', port=8000)
4

1 回答 1

3

这是您在上传文件准备好发送到 OpenERP 之前必须做的事情

获取文件指针

photo  = request.files.get('photo')

读取文件数据

photo_data = photo.file.read()

用 base64 编码

base64.b64encode(photo_data)

现在您可以将此数据传递给 openerp

于 2013-05-29T07:04:50.457 回答