coercing to Unicode: need string or buffer, FieldFile found Django


When I try to send Content File in Django, I got this error :

1
xmlrpclib.Fault: <Fault 1: "<type ‘exceptions.TypeError’>:coercing to Unicode: need string or buffer, FieldFile found">

Then i suddenly that making a dumb mistake. Watch this codes :

1
2
3
4
ins = Insurance.objects.get(id=arg)
dict_ins = model_to_dict(at, fields=("id", "filename", "filesize"))
dict_ins[‘file_insurance’] = xmlrpclib.Binary(open(ins.file).read())
insurances.append(dict_at)

Nothing wrong? You can’t find what the problem cause ? Here are :

1
xmlrpclib.Binary(open(ins.file).read())

Yes, I use “ins.file” which it’s FileField type. If you want to read from full path of FileField, then you should read it by “ins.file.path”.

1
2
3
4
ins = Insurance.objects.get(id=arg)
dict_ins = model_to_dict(at, fields=("id", "filename", "filesize"))
dict_ins[‘file_insurance’] = xmlrpclib.Binary(open(ins.file.path).read())
insurances.append(dict_at)

Sometimes we missing a little dot in the paragraph 🙂


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.