Sometimes, in Django model, an ORM object that is queried directly to the JSON will have an error:
TypeError is not JSON serializable
def convert_to_builtin_type (obj):
# print ' Default (', repr (obj), ') '
# Convert objects to a dictionary of Btheir representation
D = {' __class__ ': obj.__class__.__name__,
' __module__ ': obj.__module__,
}
D.update (obj.__dict__)
Return D
And then call in the function:
ip = Ip.objects.filter (IP=IP)
Context = {' IP ': List (IP)}
Return HttpResponse (Json.dumps, Ensure_ascii=false, indent=4, Default=convert_to_builtin_type)
The entries in the filter out Ip=ip are stored in the IP variable and then formatted to be stored in the context variable. Placed in default when called.
If you like Pythonic, you can use the following lambda way to fix:
Return HttpResponse (Json.dumps, Ensure_ascii=false, indent=4, Default=lambda o:o.__dict__))