标签:coder syntax ack important space rda elf 查询 bit
关于Django中的序列化主要应用在将数据库中检索的数据返回给客户端用户,特别的Ajax请求一般返回的为Json格式。
1
2
3
4
5
|
from django.core import serializers ret = models.BookType.objects. all () data = serializers.serialize( "json" , ret) |
1
2
3
4
5
6
7
8
|
import json #ret = models.BookType.objects.all().values(‘caption‘) ret = models.BookType.objects. all ().values_list( ‘caption‘ ) ret = list (ret) result = json.dumps(ret) |
from datetime import datetime from datetime import date import json class JsonCustomEncoder(json.JSONEncoder): def default(self, field): if isinstance(field, datetime): # 自定义对datetime做处理 return field.strftime(‘%Y-%m-%d %H:%M:%S‘) elif isinstance(field, date): # 自定义对date做处理 return field.strftime(‘%Y-%m-%d‘) else: return json.JSONEncoder.default(self, field) data_list = [ {‘name‘:‘alex‘,‘age‘:19, ‘ctime‘: datetime.now()}, {‘name‘:‘egon‘,‘age‘:19, ‘ctime‘: datetime.now()}, {‘name‘:‘eric‘,‘age‘:19, ‘ctime‘: datetime.now()}, {‘name‘:‘rain‘,‘age‘:19, ‘ctime‘: datetime.now()} ] val = json.dumps(data_list,cls=JsonCustomEncoder) print(val)
标签:coder syntax ack important space rda elf 查询 bit
原文地址:http://www.cnblogs.com/lucaq/p/7587108.html