rest_framework serializers 使用(多条
2018-06-23 本文已影响0人
鸟它鸟
In [1]: from idcs.models import Idc
In [2]: Idc.objects.all()
Out[2]: <QuerySet [<Idc: 酒仙桥机房>, <Idc: 亦庄机房>, <Idc: 亦庄机房>, <Idc: 亦庄机房>, <Idc: 亦庄机房>, <Idc: 亦庄机房>, <Idc: 亦庄机房>, <Idc: 亦庄机房>, <Idc: 亦庄机房>, <Idc: 亦庄机房>]>
In [3]: from idcs.serializers import IdcSerializer
In [4]: datas=IdcSerializer(Idc.objects.all(),many=True)
#传入一个queryset,同时加一个参数many=True即可,使用方式与单个数据相同
In [5]: datas
Out[5]:
IdcSerializer(<QuerySet [<Idc: 酒仙桥机房>, <Idc: 亦庄机房>, <Idc: 亦庄机房>, <Idc: 亦庄机房>, <Idc: 亦庄机房>, <Idc: 亦庄机房>, <Idc: 亦庄机房>, <Idc: 亦庄机房>, <Idc: 亦庄机房>, <Idc: 亦庄机房>]>, many=True):
id = IntegerField(read_only=True)
name = CharField(max_length=32, required=True)
address = CharField(max_length=256, required=True)
phone = CharField(max_length=15, required=True)
email = EmailField(required=True)
letter = CharField(max_length=5, required=True)
In [6]: datas.data
Out[6]: [OrderedDict([('id', 2), ('name', '酒仙桥机房'), ('address', '北京酒仙桥'), ('phone', '12345678'), ('email', 'rock@51reboot.com'), ('letter', 'jxq')]), OrderedDict([('id', 3), ('name', '亦庄机房'), ('address', '北京亦庄机房'), ('phone', '12345678'), ('email', 'rock@51reboot.com'), ('letter', 'yz')]), OrderedDict([('id', 4), ('name', '亦庄机房'), ('address', '北京亦庄机房'), ('phone', '12345678'), ('email', 'rock@51reboot.com'), ('letter', 'yz')]), OrderedDict([('id', 5), ('name', '亦庄机房'), ('address', '北京亦庄机房'), ('phone', '12345678'), ('email', 'rock@51reboot.com'), ('letter', 'yz')]), OrderedDict([('id', 6), ('name', '亦庄机房'), ('address', '北京亦庄机房'), ('phone', '12345678'), ('email', 'rock222@51reboot.com'), ('letter', 'yz')]), OrderedDict([('id', 7), ('name', '亦庄机房'), ('address', '北京亦庄机房'), ('phone', '12345678'), ('email', 'rock@51reboot.com'), ('letter', 'yz')]), OrderedDict([('id', 8), ('name', '亦庄机房'), ('address', '北京亦庄机房'), ('phone', '12345678'), ('email', 'rock@51reboot.com'), ('letter', 'yz')]), OrderedDict([('id', 9), ('name', '亦庄机房'), ('address', '北京亦庄机房'), ('phone', '12345678'), ('email', 'rock222@51reboot.com'), ('letter', 'yz')]), OrderedDict([('id', 10), ('name', '亦庄机房'), ('address', '北京亦庄机房'), ('phone', '12345678'), ('email', 'rock222@51reboot.com'), ('letter', 'yz')]), OrderedDict([('id', 11), ('name', '亦庄机房'), ('address', '北京亦庄机房'), ('phone', '12345678'), ('email', 'rock222@51reboot.com'), ('letter', 'yz')])]
转json
In [7]: from rest_framework.renderers import JSONRenderer
In [8]: JSONRenderer().render(datas.data)
Out[8]: b'[{"id":2,"name":"\xe9\x85\x92\xe4\xbb\x99\xe6\xa1\xa5\xe6\x9c\xba\xe6\x88\xbf","address":"\xe5\x8c\x97\xe4\xba\xac\xe9\x85\x92\xe4\xbb\x99\xe6\xa1\xa5","phone":"12345678","email":"rock@51reboot.com","letter":"jxq"},{"id":3,"name":"\xe4\xba\xa6\xe5\xba\x84\xe6\x9c\xba\xe6\x88\xbf","address":"\xe5\x8c\x97\xe4\xba\xac\xe4\xba\xa6\xe5\xba\x84\xe6\x9c\xba\xe6\x88\xbf","phone":"12345678","email":"rock@51reboot.com","letter":"yz"},{"id":4,"name":"\xe4\xba\xa6\xe5\xba\x84\xe6\x9c\xba\xe6\x88\xbf","address":"\xe5\x8c\x97\xe4\xba\xac\xe4\xba\xa6\xe5\xba\x84\xe6\x9c\xba\xe6\x88\xbf","phone":"12345678","email":"rock@51reboot.com","letter":"yz"},{"id":5,"name":"\xe4\xba\xa6\xe5\xba\x84\xe6\x9c\xba\xe6\x88\xbf","address":"\xe5\x8c\x97\xe4\xba\xac\xe4\xba\xa6\xe5\xba\x84\xe6\x9c\xba\xe6\x88\xbf","phone":"12345678","email":"rock@51reboot.com","letter":"yz"},{"id":6,"name":"\xe4\xba\xa6\xe5\xba\x84\xe6\x9c\xba\xe6\x88\xbf","address":"\xe5\x8c\x97\xe4\xba\xac\xe4\xba\xa6\xe5\xba\x84\xe6\x9c\xba\xe6\x88\xbf","phone":"12345678","email":"rock222@51reboot.com","letter":"yz"},{"id":7,"name":"\xe4\xba\xa6\xe5\xba\x84\xe6\x9c\xba\xe6\x88\xbf","address":"\xe5\x8c\x97\xe4\xba\xac\xe4\xba\xa6\xe5\xba\x84\xe6\x9c\xba\xe6\x88\xbf","phone":"12345678","email":"rock@51reboot.com","letter":"yz"},{"id":8,"name":"\xe4\xba\xa6\xe5\xba\x84\xe6\x9c\xba\xe6\x88\xbf","address":"\xe5\x8c\x97\xe4\xba\xac\xe4\xba\xa6\xe5\xba\x84\xe6\x9c\xba\xe6\x88\xbf","phone":"12345678","email":"rock@51reboot.com","letter":"yz"},{"id":9,"name":"\xe4\xba\xa6\xe5\xba\x84\xe6\x9c\xba\xe6\x88\xbf","address":"\xe5\x8c\x97\xe4\xba\xac\xe4\xba\xa6\xe5\xba\x84\xe6\x9c\xba\xe6\x88\xbf","phone":"12345678","email":"rock222@51reboot.com","letter":"yz"},{"id":10,"name":"\xe4\xba\xa6\xe5\xba\x84\xe6\x9c\xba\xe6\x88\xbf","address":"\xe5\x8c\x97\xe4\xba\xac\xe4\xba\xa6\xe5\xba\x84\xe6\x9c\xba\xe6\x88\xbf","phone":"12345678","email":"rock222@51reboot.com","letter":"yz"},{"id":11,"name":"\xe4\xba\xa6\xe5\xba\x84\xe6\x9c\xba\xe6\x88\xbf","address":"\xe5\x8c\x97\xe4\xba\xac\xe4\xba\xa6\xe5\xba\x84\xe6\x9c\xba\xe6\x88\xbf","phone":"12345678","email":"rock222@51reboot.com","letter":"yz"}]'