我试图在Django Rest Framework的帮助下将我的Django应用程序连接到mailchimps API,如果我想创建一个批处理操作,我需要发送以下调用:
{
"operations": [
{
"method": "PUT",
"path": "lists/abc123/members/817f1571000f0b843c2b8a6982813db2",
"body": "{\"email_address\":\"hall@hallandoates.com\", \"status_if_new\":\"subscribed\"}"
},...
]
}
如您所见,body对象应该是json字符串。为了创建这些调用,我创建了一个模型操作:
models.py
class Operation(object):
def __init__(self, method, list, contact):
email_clean = contact.email_address.lower().encode('utf-8')
subscriber_hash = hashlib.md5(email_clean).hexdigest()
serializer = ContactSerializer(contact)
body = serializer.data
self.method = method
self.path = "lists/%s/members/%s" % (list, subscriber_hash)
self.body = body
以下序列化程序:
serializer.py
class OperationSerializer(serializers.Serializer):
method = serializers.CharField()
path = serializers.CharField()
body = serializers.CharField(allow_blank=True, required=False)
当我使用序列化程序生成JSON并使用JSONRenderer()解析数据时,将返回以下调用:
{
"operations": [
{
"method": "PUT",
"path": "lists/abc123/members/817f1571000f0b843c2b8a6982813db2",
"body": "{\'email_address\':\'hall@hallandoates.com\', \'status_if_new\':\'subscribed\'}"
},...
]
}
这个电话因单引号而中断,任何人都可以帮我解决这个问题吗?