1

I created an api using python bottle for accessing OpenERP REST API.

My problem is while uploading a image file and writing it into the binary field in OpenERP

it throws raise TypeError, "cannot marshal None unless allow_none is enabled"

Here I uploaded my code

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)

1 Answer 1

3

This are the things you have to do before the uploaded file is ready to send to OpenERP

get the file pointer

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

read the file data

photo_data = photo.file.read()

encode with base64

base64.b64encode(photo_data)

Now you can pass this data to openerp

Sign up to request clarification or add additional context in comments.

2 Comments

cannot encode the image when i printed the return type of image it returns as instance type why it returns as instance and how to return it as binary data
Thanks sinoj for great...I want to have reverse of above question. I want to read image from xml rpc of openerp. I have created custom module in openerp v7 and I want to fetch value of that module in my website using django. I am able to fetch all text field, but not able to get image. While fetching image (binary field) it gives value probably b64encoded. I don't know to make that image visible without storing image data. Is there any direct way to access image url of openerp module. Can you please guide me ? Thanks for your time and help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.