简体   繁体   中英

Object of type date is not JSON Serializable

I have a table in MySQL with name, period, indicator, value as the column names. Period stores date. I am trying to get the object from django using the following code

def get_table(request):

    tab = list(table.objects.values('name', 'period' , 'indicator', 'value'))
    json_data = json.dumps(tab)

    return HttpResponse(json_data)

I receive an error saying date is not JSON Serializable. How do I convert it to string before getting the object? Can anyone help me with the code?

did you try django serialization :

from django.core import serializers

fields = ['name', 'period' , 'indicator', 'value']
qs = table.objects.all()
json_data = serializers.serialize('json', qs, fields=fields)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM