alorence / django-modern-rpc

Simple XML-RPC and JSON-RPC server for modern Django
http://django-modern-rpc.rtfd.io
MIT License
98 stars 17 forks source link

How to specify XML Serializer? #22

Closed melvinkcx closed 5 years ago

melvinkcx commented 6 years ago

I am trying to access Django Model, but it gave me an InternalError. what's happening on server side is simple retrieve a user model and return to the client.

I would like to try using Django XMLSerializer but have no idea how to configure.

  File "/usr/lib64/python3.6/xmlrpc/client.py", line 1112, in __call__
    return self.__send(self.__name, args)
  File "/usr/lib64/python3.6/xmlrpc/client.py", line 1452, in __request
    verbose=self.__verbose
  File "/usr/lib64/python3.6/xmlrpc/client.py", line 1154, in request
    return self.single_request(host, handler, request_body, verbose)
  File "/usr/lib64/python3.6/xmlrpc/client.py", line 1170, in single_request
    return self.parse_response(resp)
  File "/usr/lib64/python3.6/xmlrpc/client.py", line 1342, in parse_response
    return u.close()
  File "/usr/lib64/python3.6/xmlrpc/client.py", line 656, in close
    raise Fault(**self._stack[0])
xmlrpc.client.Fault: <Fault -32603: 'Internal error: Unable to serialize result as valid XML: encode() argument 1 must be string, not None'>
>>> 
alorence commented 6 years ago

Currently, there is no way to configure the class used to serialize data in XML Handler. This job is done by (undocumented) builtin class xmlrpc.client.Marshaller (python 3) or xmlrpclib.Marshaller (python 2). This class can serialize any object with __dict__ attribute, so Django model instances can be returned directly by a RPC Method.

This will work with XML-RPC, but not with JSON-RPC, that's why this kind of use is not supported officially. It is your responsibility to serialize your data to something supported (basic types, list, dict, see documentation for a full list).