我使用python瓶创建了一个api来访问OpenERP REST API。
我的问题是上传图像文件并将其写入OpenERP
中的二进制字段它抛出引发的TypeError,“除非启用了allow_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)
答案 0 :(得分:2)
在上传的文件准备好发送到OpenERP
之前,您需要做的事情获取文件指针
photo = request.files.get('photo')
读取文件数据
photo_data = photo.file.read()
使用base64进行编码
base64.b64encode(photo_data)
现在您可以将此数据传递给openerp