Send content file from FileField Django models via XMLRPC


Common cases, we need to send content file from FileField from django models via XML-RPC. In this example, I use django-xmlrpc to handle XMLRPC server. First, we have models :

1
2
3
4
from django.db import models

class Insurance(models.Model):
    file = models.FileField()


Now, we need to send it file content in FieldField with django-xmlrpc. Also, we need to convert our queryset into dictionary using “model_to_dict” :

1
2
3
4
5
6
import xmlrpclib
from insurance.models import Insurance

insurance = Insurance.objects.get(id=1)
dict_insurance = model_to_dict(at, fields=("id"))
dict_insurance[‘file’] = xmlrpclib.Binary(open(at.file.path).read())

Remember to use “file.path”, if you use “file” on FileField then you will got error :

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

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.