def form_encode(obj,
instanceName):
"""Encodes a model in a form-encoded manner such as person[Name]
Args:
obj (object): The given Object to form encode.
instanceName (string): The base name to appear before each entry
for this object.
Returns:
dict: A dictionary of form encoded properties of the model.
"""
# Resolve the names first
value = APIHelper.resolve_name(obj)
retval = dict()
if value is None:
return None
# Loop through every item we need to send
for item in value:
if isinstance(value[item], list):
# Loop through each item in the list and add it by number
i = 0
for entry in value[item]:
retval.update(APIHelper.form_encode(entry, instanceName + "[" + item + "][" + str(i) + "]"))
i += 1
elif isinstance(value[item], dict):
# Loop through each item in the dictionary and add it
retval.update(APIHelper.form_encode(value[item], instanceName + "[" + item + "]"))
else:
# Add the current item
retval[instanceName + "[" + item + "]"] = value[item]
return retval
评论列表
文章目录